ws_shutdown_reason_commands_h.erl 927 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. %% This module sends the process pid to the test pid
  2. %% found in the x-test-pid header, then changes the
  3. %% shutdown reason and closes the connection normally.
  4. -module(ws_shutdown_reason_commands_h).
  5. -behavior(cowboy_websocket).
  6. -export([init/2]).
  7. -export([websocket_init/1]).
  8. -export([websocket_handle/2]).
  9. -export([websocket_info/2]).
  10. init(Req, RunOrHibernate) ->
  11. TestPid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
  12. {cowboy_websocket, Req, {TestPid, RunOrHibernate}}.
  13. websocket_init(State={TestPid, RunOrHibernate}) ->
  14. TestPid ! {ws_pid, self()},
  15. ShutdownReason = receive
  16. {TestPid, SR} ->
  17. SR
  18. after 1000 ->
  19. error(timeout)
  20. end,
  21. Commands = [
  22. {shutdown_reason, ShutdownReason},
  23. close
  24. ],
  25. case RunOrHibernate of
  26. run -> {Commands, State};
  27. hibernate -> {Commands, State, hibernate}
  28. end.
  29. websocket_handle(_, State) ->
  30. {[], State}.
  31. websocket_info(_, State) ->
  32. {[], State}.