http_handler_long_polling.erl 561 B

12345678910111213141516171819202122
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_handler_long_polling).
  3. -behaviour(cowboy_http_handler).
  4. -export([init/3, handle/2, info/3, terminate/2]).
  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(_Req, _State) ->
  17. ok.