loop_handler_endless_h.erl 640 B

12345678910111213141516171819202122232425
  1. %% This module implements a loop handler that streams endless data.
  2. -module(loop_handler_endless_h).
  3. -export([init/2]).
  4. -export([info/3]).
  5. init(Req0, #{delay := Delay} = Opts) ->
  6. case cowboy_req:header(<<"x-test-pid">>, Req0) of
  7. BinPid when is_binary(BinPid) ->
  8. Pid = list_to_pid(binary_to_list(BinPid)),
  9. Pid ! {Pid, self(), init},
  10. ok;
  11. _ ->
  12. ok
  13. end,
  14. erlang:send_after(Delay, self(), timeout),
  15. Req = cowboy_req:stream_reply(200, Req0),
  16. {cowboy_loop, Req, Opts}.
  17. info(timeout, Req, State) ->
  18. cowboy_req:stream_body(<<0:10000/unit:8>>, nofin, Req),
  19. %% Equivalent to a 0 timeout.
  20. self() ! timeout,
  21. {ok, Req, State}.