summaryrefslogtreecommitdiffstats
path: root/examples/clock/src/toppage_handler.erl
blob: b9c650496e4b8b5997e6858facee72b02a8dfb72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%% 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/3]).

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>Current time (best source): <span id=\"time_best\">unknown</span>
		<span> </span><span id=\"status_best\">unknown</span></p>
	<p>Current time (websocket only): <span id=\"time_websocket\">unknown</span>
		<span> </span><span id=\"status_websocket\">unknown</span></p>
	<p>Current time (eventsource only): <span id=\"time_eventsource\">unknown</span>
		<span> </span><span id=\"status_eventsource\">unknown</span></p>
	<p>Current time (polling only): <span id=\"time_polling\">unknown</span>
		<span> </span><span id=\"status_polling\">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 start = function(name, options) {
		var bullet = $.bullet('ws://localhost:8080/bullet', options);
	 	bullet.onopen = function(){
			$('#status_' + name).text('online');
		};
		bullet.ondisconnect = function(){
			$('#status_' + name).text('offline');
		};
		bullet.onmessage = function(e){
			if (e.data != 'pong'){
				$('#time_' + name).text(e.data);
			}
		};
		bullet.onheartbeat = function(){
			console.log('ping: ' + name);
			bullet.send('ping');
		}
	};

	start('best', {});
	start('websocket', {'disableEventSource': true,
		'disableXHRPolling': true});
	start('eventsource', {'disableWebSocket': true,
		'disableXHRPolling': true});
	start('polling', {'disableWebSocket': true,
		'disableEventSource': true});
});
// ]]>
	</script>
</body>
</html>
">>,
	{ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],
		Body, Req),
	{ok, Req2, State}.

terminate(_Reason, _Req, _State) ->
	ok.