1%% This module implements a loop handler for long-polling. 2%% It starts by sending itself a message after 200ms, 3%% then sends another after that for a total of 3 messages. 4%% When it receives the last message, it sends a 102 reply back. 5 6-module(long_polling_h). 7-behaviour(cowboy_loop_handler). 8 9-export([init/3]). 10-export([info/3]). 11-export([terminate/3]). 12 13init(_, Req, _) -> 14 erlang:send_after(200, self(), timeout), 15 {loop, Req, 2, 5000, hibernate}. 16 17info(timeout, Req, 0) -> 18 {ok, Req2} = cowboy_req:reply(102, Req), 19 {ok, Req2, 0}; 20info(timeout, Req, Count) -> 21 erlang:send_after(200, self(), timeout), 22 {loop, Req, Count - 1, hibernate}. 23 24terminate({normal, shutdown}, _, 0) -> 25 ok; 26terminate({error, overflow}, _, _) -> 27 ok. 28