ws_init_commands_h.erl 823 B

123456789101112131415161718192021222324252627282930
  1. %% This module takes commands from the x-commands header
  2. %% and returns them in the websocket_init/1 callback.
  3. -module(ws_init_commands_h).
  4. -behavior(cowboy_websocket).
  5. -export([init/2]).
  6. -export([websocket_init/1]).
  7. -export([websocket_handle/2]).
  8. -export([websocket_info/2]).
  9. init(Req=#{pid := Pid}, RunOrHibernate) ->
  10. Commands0 = cowboy_req:header(<<"x-commands">>, Req),
  11. Commands = binary_to_term(base64:decode(Commands0)),
  12. case Commands of
  13. bad -> ct_helper_error_h:ignore(Pid, cowboy_websocket, handler_call, 6);
  14. _ -> ok
  15. end,
  16. {cowboy_websocket, Req, {Commands, RunOrHibernate}}.
  17. websocket_init(State={Commands, run}) ->
  18. {Commands, State};
  19. websocket_init(State={Commands, hibernate}) ->
  20. {Commands, State, hibernate}.
  21. websocket_handle(_, State) ->
  22. {[], State}.
  23. websocket_info(_, State) ->
  24. {[], State}.