aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/sse_clock_h.erl
diff options
context:
space:
mode:
Diffstat (limited to 'test/handlers/sse_clock_h.erl')
-rw-r--r--test/handlers/sse_clock_h.erl21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/handlers/sse_clock_h.erl b/test/handlers/sse_clock_h.erl
new file mode 100644
index 0000000..23b834f
--- /dev/null
+++ b/test/handlers/sse_clock_h.erl
@@ -0,0 +1,21 @@
+%% This module implements a loop handler that sends
+%% the current time every second using SSE.
+
+-module(sse_clock_h).
+
+-export([init/2]).
+-export([info/3]).
+
+init(Req, State) ->
+ self() ! timeout,
+ {cowboy_loop, cowboy_req:stream_reply(200, #{
+ <<"content-type">> => <<"text/event-stream">>
+ }, Req), State}.
+
+info(timeout, Req, State) ->
+ erlang:send_after(1000, self(), timeout),
+ Time = calendar:system_time_to_rfc3339(erlang:system_time(second)),
+ cowboy_req:stream_events(#{
+ data => Time
+ }, nofin, Req),
+ {ok, Req, State}.