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/toppage_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/toppage_handler.erl')
-rw-r--r-- | examples/clock/src/toppage_handler.erl | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/clock/src/toppage_handler.erl b/examples/clock/src/toppage_handler.erl new file mode 100644 index 0000000..66c8400 --- /dev/null +++ b/examples/clock/src/toppage_handler.erl @@ -0,0 +1,60 @@ +%% Feel free to use, reuse and abuse the code in this file. + +%% @doc Main page of the clock application. +-module(toppage_handler). + +-export([init/3]). +-export([handle/2]). +-export([terminate/2]). + +init(_Transport, Req, []) -> + {ok, Req, undefined}. + +handle(Req, State) -> + Body = <<" +<!DOCTYPE html> +<html lang=\"en\"> +<head> + <meta charset=\"utf-8\"> + <title>Bullet Clock</title> +</head> + +<body> + <p>Connection status: <span id=\"status\">bullet not started</span></p> + <p>Current time: <span id=\"time\">unknown</span></p> + + <script + src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js\"> + </script> + <script src=\"/static/bullet.js\"></script> + <script type=\"text/javascript\"> +// <![CDATA[ +$(document).ready(function(){ + var bullet = $.bullet('ws://localhost:8080/bullet'); + bullet.onopen = function(){ + $('#status').text('online'); + }; + bullet.ondisconnect = function(){ + $('#status').text('offline'); + }; + bullet.onmessage = function(e){ + if (e.data != 'pong'){ + $('#time').text(e.data); + } + }; + bullet.onheartbeat = function(){ + console.log('ping'); + bullet.send('ping'); + } +}); +// ]]> + </script> +</body> +</html> +">>, + {ok, Req2} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}], + Body, Req), + {ok, Req2, State}. + +terminate(_Req, _State) -> + ok. |