mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Fix websocket example
This commit is contained in:
parent
352cfce663
commit
a4fb56018e
2 changed files with 32 additions and 2 deletions
|
@ -17,8 +17,9 @@ start(_Type, _Args) ->
|
||||||
{"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
|
{"/static/[...]", cowboy_static, {priv_dir, websocket, "static"}}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
|
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
|
||||||
[{env, [{dispatch, Dispatch}]}]),
|
env => #{dispatch => Dispatch}
|
||||||
|
}),
|
||||||
websocket_sup:start_link().
|
websocket_sup:start_link().
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
|
|
|
@ -249,3 +249,32 @@ do_markdown_middleware(Transport, Protocol, Config) ->
|
||||||
{200, Headers, <<"<h1>", _/bits >>} = do_get(Transport, Protocol, "/video.html", Config),
|
{200, Headers, <<"<h1>", _/bits >>} = do_get(Transport, Protocol, "/video.html", Config),
|
||||||
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
{_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
%% Websocket.
|
||||||
|
|
||||||
|
websocket(_) ->
|
||||||
|
doc("Websocket example."),
|
||||||
|
try
|
||||||
|
do_compile_and_start(websocket),
|
||||||
|
%% We can only initiate a Websocket connection from HTTP/1.1.
|
||||||
|
{ok, Pid} = gun:open("127.0.0.1", 8080, #{protocols => [http], retry => 0}),
|
||||||
|
{ok, http} = gun:await_up(Pid),
|
||||||
|
_ = monitor(process, Pid),
|
||||||
|
gun:ws_upgrade(Pid, "/websocket", [], #{compress => true}),
|
||||||
|
receive
|
||||||
|
{gun_ws_upgrade, Pid, ok, _} ->
|
||||||
|
ok;
|
||||||
|
Msg1 ->
|
||||||
|
exit({connection_failed, Msg1})
|
||||||
|
end,
|
||||||
|
gun:ws_send(Pid, {text, <<"hello">>}),
|
||||||
|
receive
|
||||||
|
{gun_ws, Pid, {text, <<"That's what she said! hello">>}} ->
|
||||||
|
ok;
|
||||||
|
Msg2 ->
|
||||||
|
exit({receive_failed, Msg2})
|
||||||
|
end,
|
||||||
|
gun:ws_send(Pid, close)
|
||||||
|
after
|
||||||
|
do_stop(websocket)
|
||||||
|
end.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue