mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 04:10:24 +00:00
Fix eventsource example
This commit is contained in:
parent
84fb85e2e4
commit
5003bae369
3 changed files with 29 additions and 5 deletions
|
@ -17,9 +17,9 @@ start(_Type, _Args) ->
|
|||
{"/", cowboy_static, {priv_file, eventsource, "index.html"}}
|
||||
]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
|
||||
{env, [{dispatch, Dispatch}]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
|
||||
env => #{dispatch => Dispatch}
|
||||
}),
|
||||
eventsource_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
|
|
|
@ -7,8 +7,9 @@
|
|||
-export([info/3]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
Headers = [{<<"content-type">>, <<"text/event-stream">>}],
|
||||
Req2 = cowboy_req:chunked_reply(200, Headers, Req),
|
||||
Req2 = cowboy_req:chunked_reply(200, #{
|
||||
<<"content-type">> => <<"text/event-stream">>
|
||||
}, Req),
|
||||
erlang:send_after(1000, self(), {message, "Tick"}),
|
||||
{cowboy_loop, Req2, Opts, 5000}.
|
||||
|
||||
|
|
|
@ -190,6 +190,29 @@ do_echo_post(Transport, Protocol, Config) ->
|
|||
{ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
|
||||
ok.
|
||||
|
||||
%% Eventsource.
|
||||
|
||||
eventsource(Config) ->
|
||||
doc("Eventsource example."),
|
||||
try
|
||||
do_compile_and_start(eventsource),
|
||||
do_eventsource(tcp, http, Config),
|
||||
do_eventsource(tcp, http2, Config)
|
||||
after
|
||||
do_stop(eventsource)
|
||||
end.
|
||||
|
||||
do_eventsource(Transport, Protocol, Config) ->
|
||||
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
|
||||
Ref = gun:get(ConnPid, "/eventsource"),
|
||||
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
|
||||
{_, <<"text/event-stream">>} = lists:keyfind(<<"content-type">>, 1, Headers),
|
||||
%% Receive a few events.
|
||||
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
|
||||
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
|
||||
{data, nofin, << "id: ", _/bits >>} = gun:await(ConnPid, Ref, 2000),
|
||||
gun:close(ConnPid).
|
||||
|
||||
%% REST Hello World.
|
||||
|
||||
rest_hello_world(Config) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue