diff options
author | Loïc Hoguin <[email protected]> | 2012-07-09 13:07:50 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2012-07-09 13:13:36 +0200 |
commit | 6f17bb1455574012b4a15ad780585f964d85b7f9 (patch) | |
tree | e8069fd0b4ae58b6d01e8ce7570fb3246a9938e9 /examples/clock/src/stream_handler.erl | |
parent | ffde065f4b3a4f30738a172615f78de59a9c8490 (diff) | |
download | bullet-6f17bb1455574012b4a15ad780585f964d85b7f9.tar.gz bullet-6f17bb1455574012b4a15ad780585f964d85b7f9.tar.bz2 bullet-6f17bb1455574012b4a15ad780585f964d85b7f9.zip |
Add a bullet clock example
Diffstat (limited to 'examples/clock/src/stream_handler.erl')
-rw-r--r-- | examples/clock/src/stream_handler.erl | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/examples/clock/src/stream_handler.erl b/examples/clock/src/stream_handler.erl new file mode 100644 index 0000000..4f18b9d --- /dev/null +++ b/examples/clock/src/stream_handler.erl @@ -0,0 +1,36 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Stream handler for clock synchronizing. +-module(stream_handler). + +-export([init/4]). +-export([stream/3]). +-export([info/3]). +-export([terminate/2]). + +-define(PERIOD, 1000). + +init(_Transport, Req, _Opts, _Active) -> + io:format("bullet init~n"), + _ = erlang:send_after(?PERIOD, self(), refresh), + {ok, Req, undefined}. + +stream(<<"ping">>, Req, State) -> + io:format("ping received~n"), + {reply, <<"pong">>, Req, State}; +stream(Data, Req, State) -> + io:format("stream received ~s~n", [Data]), + {ok, Req, State}. + +info(refresh, Req, State) -> + _ = erlang:send_after(?PERIOD, self(), refresh), + DateTime = cowboy_clock:rfc1123(), + io:format("clock refresh timeout: ~s~n", [DateTime]), + {reply, DateTime, Req, State}; +info(Info, Req, State) -> + io:format("info received ~p~n", [Info]), + {ok, Req, State}. + +terminate(_Req, _State) -> + io:format("bullet terminate~n"), + ok. |