long_polling_h.erl 680 B

12345678910111213141516171819202122232425
  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. -module(long_polling_h).
  6. -export([init/2]).
  7. -export([info/3]).
  8. -export([terminate/3]).
  9. init(Req, _) ->
  10. erlang:send_after(200, self(), timeout),
  11. {cowboy_loop, Req, 2, 5000, hibernate}.
  12. info(timeout, Req, 0) ->
  13. {shutdown, cowboy_req:reply(102, Req), 0};
  14. info(timeout, Req, Count) ->
  15. erlang:send_after(200, self(), timeout),
  16. {ok, Req, Count - 1, hibernate}.
  17. terminate(shutdown, _, 0) ->
  18. ok;
  19. terminate({error, overflow}, _, _) ->
  20. ok.