http_long_polling.erl 608 B

123456789101112131415161718192021222324
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_long_polling).
  3. -behaviour(cowboy_http_handler).
  4. -export([init/3, handle/2, info/3, terminate/3]).
  5. init({_Transport, http}, Req, _Opts) ->
  6. erlang:send_after(500, self(), timeout),
  7. {loop, Req, 5, 5000, hibernate}.
  8. handle(_Req, _State) ->
  9. exit(badarg).
  10. info(timeout, Req, 0) ->
  11. {ok, Req2} = cowboy_req:reply(102, Req),
  12. {ok, Req2, 0};
  13. info(timeout, Req, State) ->
  14. erlang:send_after(500, self(), timeout),
  15. {loop, Req, State - 1, hibernate}.
  16. terminate({normal, shutdown}, _, _) ->
  17. ok;
  18. terminate({error, overflow}, _, _) ->
  19. ok.