index.html (1107B)
1 <!doctype>
2 <html>
3 <head>
4 <script>
5 var source = new EventSource("/subscribe");
6 source.addEventListener("message", function(evt) {
7 console.log(evt.data);
8 });
9
10 source.addEventListener("join", function(evt) {
11 console.log("join event: " + evt.data);
12 prepend("join event");
13 });
14
15 source.addEventListener("leave", function(evt) {
16 console.log("leave event: " + evt.data);
17 prepend("leave event");
18 });
19
20 source.addEventListener("ping", function(evt) {
21 console.log("got ping");
22 prepend("ping event");
23 });
24
25 source.addEventListener("open", function(evt) {
26 console.log("connected");
27 prepend("connected");
28 });
29
30 source.addEventListener("error", function(evt) {
31 console.log("error");
32 prepend("error");
33 });
34
35 function prepend(txt) {
36 var obj = document.getElementById("events");
37 var now = new Date();
38
39 var time = now.getFullYear() + "/" + (now.getMonth() + 1) + "/" +
40 now.getDate() + " " + now.getHours() + ":" + now.getMinutes() +
41 ":" + now.getSeconds();
42
43 obj.innerHTML = "<p>" + time + " - " + txt + "</p>" + obj.innerHTML;
44 }
45
46 </script>
47
48 <body>
49 <h3>events</h3>
50 <div id="events">
51 </div>
52 </body>
53
54 </html>