| 1 |
%% This module takes commands from the x-commands header |
| 2 |
%% and returns them in the websocket_init/1 callback. |
| 3 |
|
| 4 |
-module(ws_init_commands_h). |
| 5 |
-behavior(cowboy_websocket). |
| 6 |
|
| 7 |
-export([init/2]). |
| 8 |
-export([websocket_init/1]). |
| 9 |
-export([websocket_handle/2]). |
| 10 |
-export([websocket_info/2]). |
| 11 |
|
| 12 |
init(Req, Opts) -> |
| 13 |
Commands0 = cowboy_req:header(<<"x-commands">>, Req), |
| 14 |
Commands = binary_to_term(base64:decode(Commands0)), |
| 15 |
case Commands of |
| 16 |
bad -> |
| 17 |
Pid = case Req of |
| 18 |
#{version := 'HTTP/2'} -> self(); |
| 19 |
#{pid := Pid0} -> Pid0 |
| 20 |
end, |
| 21 |
ct_helper_error_h:ignore(Pid, cowboy_websocket, handler_call, 6); |
| 22 |
_ -> |
| 23 |
ok |
| 24 |
end, |
| 25 |
{cowboy_websocket, Req, {Commands, maps:get(run_or_hibernate, Opts)}, Opts}. |
| 26 |
|
| 27 |
websocket_init(State={Commands, run}) -> |
| 28 |
{Commands, State}; |
| 29 |
websocket_init(State={Commands, hibernate}) -> |
| 30 |
{Commands, State, hibernate}. |
| 31 |
|
| 32 |
websocket_handle(_, State) -> |
| 33 |
{[], State}. |
| 34 |
|
| 35 |
websocket_info(_, State) -> |
| 36 |
{[], State}. |