ws_info_commands_h.erl 888 B

1234567891011121314151617181920212223242526272829303132
  1. %% This module takes commands from the x-commands header
  2. %% and returns them in the websocket_info/2 callback.
  3. %% This callback is triggered via a message.
  4. -module(ws_info_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=#{pid := Pid}, RunOrHibernate) ->
  11. Commands0 = cowboy_req:header(<<"x-commands">>, Req),
  12. Commands = binary_to_term(base64:decode(Commands0)),
  13. case Commands of
  14. bad -> ct_helper_error_h:ignore(Pid, cowboy_websocket, handler_call, 6);
  15. _ -> ok
  16. end,
  17. {cowboy_websocket, Req, {Commands, RunOrHibernate}}.
  18. websocket_init(State) ->
  19. self() ! shoot,
  20. {[], State}.
  21. websocket_handle(_, State) ->
  22. {[], State}.
  23. websocket_info(_, State={Commands, run}) ->
  24. {Commands, State};
  25. websocket_info(_, State={Commands, hibernate}) ->
  26. {Commands, State, hibernate}.