mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Add shutdown_reason Websocket command
This allows changing the normal exit reason of Websocket processes, providing a way to signal other processes of why the exit occurred.
This commit is contained in:
parent
cc54c207e3
commit
d52e84bdd9
5 changed files with 87 additions and 6 deletions
38
test/handlers/ws_shutdown_reason_commands_h.erl
Normal file
38
test/handlers/ws_shutdown_reason_commands_h.erl
Normal file
|
@ -0,0 +1,38 @@
|
|||
%% This module sends the process pid to the test pid
|
||||
%% found in the x-test-pid header, then changes the
|
||||
%% shutdown reason and closes the connection normally.
|
||||
|
||||
-module(ws_shutdown_reason_commands_h).
|
||||
-behavior(cowboy_websocket).
|
||||
|
||||
-export([init/2]).
|
||||
-export([websocket_init/1]).
|
||||
-export([websocket_handle/2]).
|
||||
-export([websocket_info/2]).
|
||||
|
||||
init(Req, RunOrHibernate) ->
|
||||
TestPid = list_to_pid(binary_to_list(cowboy_req:header(<<"x-test-pid">>, Req))),
|
||||
{cowboy_websocket, Req, {TestPid, RunOrHibernate}}.
|
||||
|
||||
websocket_init(State={TestPid, RunOrHibernate}) ->
|
||||
TestPid ! {ws_pid, self()},
|
||||
ShutdownReason = receive
|
||||
{TestPid, SR} ->
|
||||
SR
|
||||
after 1000 ->
|
||||
error(timeout)
|
||||
end,
|
||||
Commands = [
|
||||
{shutdown_reason, ShutdownReason},
|
||||
close
|
||||
],
|
||||
case RunOrHibernate of
|
||||
run -> {Commands, State};
|
||||
hibernate -> {Commands, State, hibernate}
|
||||
end.
|
||||
|
||||
websocket_handle(_, State) ->
|
||||
{[], State}.
|
||||
|
||||
websocket_info(_, State) ->
|
||||
{[], State}.
|
Loading…
Add table
Add a link
Reference in a new issue