-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventsource.html
373 lines (287 loc) · 10.7 KB
/
eventsource.html
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE HTML>
<!--
written by Nick Shin - [email protected]
the code found in this file is licensed under:
- Unlicense - http://unlicense.org/
this file is from https://github.com/nickshin/CheatSheets/
this file contains some HTML5 snippets on:
- eventsource
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reference sources:
+ http://www.html5rocks.com/en/tutorials/eventsource/basics/
o one way server side notification - lighter than websockets:
SSEs are sent over traditional HTTP. That means they do not require a
special protocol or server implementation to get working. WebSockets
on the other hand, require full-duplex connections and new Web Socket
servers to handle the protocol. In addition, Server-Sent Events have a
variety of features that WebSockets lack by design such as automatic
reconnection, event IDs, and the ability to send arbitrary events.
+ http://googlecodesamples.com/html5/sse/sse.html
o associating an ID with an event example
Setting an ID lets the browser keep track of the last event fired so that if,
the connection to the server is dropped, a special HTTP header (Last-Event-ID)
is set with the new request. This lets the browser determine which event is
appropriate to fire. The message event contains a e.lastEventId property.
+ http://tutorialspoint.com/html5/html5_server_sent_events.htm
o "event name" example
A single event source can generate different types events by including an
event name. If a line beginning with "event:" is present, followed by a
unique name for the event, the event is associated with that name. On the
client, an event listener can be setup to listen to that particular event.
+ see servers/sse.pl for more details on eventsource server implementation
+ the servers/sse.php can be found at:
- http://code.google.com/r/javijimenezvillar-tapquo/source/browse/www.html5rocks.com/content/tutorials/eventsource/basics/demo/sse.php
notes:
# ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING **
# ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING **
#
# poor EventSource handlers (client) may crash the browser...
#
# ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING **
# ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING ** WARNING **
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
best viewed in editor with tab stops set to 4
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
-->
<!-- ================================================== -->
<!-- START OF HTML -->
<html manifest='eventsource.appcache'>
<head>
<title>EventSource : HTML5 test code</title>
<!-- ================================================== -->
<!-- Javascript {{{ -->
<!-- ================================================== -->
<script type='text/javascript' src='js/DebugLog.js'></script>
<!-- .................................................. -->
<!-- js/DebugLog.js {{{2 -->
<!-- .................................................. -->
<script>
// for this file, replacing (hooking) the log() and throw()
// with the DEBUGLOG system
var log_hook = DEBUGLOG.printhook;
var throw_hook = DEBUGLOG.printhook;
function debuglog_onload() {
DEBUGLOG.setElementById( 'logwindow' );
}
</script>
<!-- .................................................. -->
<!-- js/DebugLog.js }}}2 -->
<!-- eventsource {{{2 -->
<!-- .................................................. -->
<script>
var EVENTSOURCE = (function(){
// ----------------------------------------
// constructor
var my = function () {
// private static unique to 'this' instance
var source = null;
// these are technically public static...
this.getsource = function () {
return ( this.classcheck() ) ? source : null;
};
this.setsource = function ( evtsrc ) {
if ( this.classcheck() )
source = evtsrc;
};
};
// ----------------------------------------
// private static
var error_code = 0;
// public static (enums) ;-)
my.EVENTSOURCE_SUCCESS = error_code--;
my.EVENTSOURCE_BAD_CLASS = error_code--;
my.EVENTSOURCE_ERNO_NOT_CONNECTED = error_code--;
my.EVENTSOURCE_ERNO_CREATE = error_code--;
// ----------------------------------------
// public static
this.className = 'EVENTSOURCE';
// ----------------------------------------
// public static
// can be overridden -- a new property is created for that instance
my.prototype = {
open: function ( url, open_cb, message_cb, error_cb, close_cb ) {
if ( ! this.classcheck() )
return EVENTSOURCE.EVENTSOURCE_BAD_CLASS;
// ........................................
var evtsrc = new EventSource( url );
if ( ! evtsrc )
return EVENTSOURCE.EVENTSOURCE_ERNO_CREATE;
evtsrc.onopen = ( open_cb ) ? open_cb : donothing;
evtsrc.onmessage = ( message_cb ) ? message_cb : donothing;
evtsrc.onerror = ( error_cb ) ? error_cb : donothing;
evtsrc.onclose = ( close_cb ) ? close_cb : donothing;
this.setsource( evtsrc );
return EVENTSOURCE.EVENTSOURCE_SUCCESS;
},
// ----------------------------------------
close: function () {
var evtsrc = this.getsource();
if ( ! evtsrc )
return EVENTSOURCE.EVENTSOURCE_ERNO_NOT_CONNECTED;
evtsrc.close();
return EVENTSOURCE.EVENTSOURCE_SUCCESS;
},
// ----------------------------------------
// house keeping
classcheck: function () {
if ( this.className === EVENTSOURCE.className )
return true;
throw_hook( "*** ERRRO: object's className mismatch" );
return false;
},
donothing: function () {} // no-op
};
return my; // must be last
}());
</script>
<script>
var MYAPP = (function () {
// ----------------------------------------
// constructor
var my = function () {
// ----------------------------------------
// 'this' collision tests
var es = null;
this.getsource = function () {
return ( this.classcheck() ) ? es : null;
};
this.setsource = function ( evtsrc ) {
if ( this.classcheck() )
es = evtsrc;
};
};
// ----------------------------------------
// private static
var error_code = 0;
// public static (enums) ;-)
my.MYAPP_SUCCESS = error_code--;
my.MYAPP_BAD_CLASS = error_code--;
my.MYAPP_ERNO_NO_EVENTSOURCE = error_code--;
// ----------------------------------------
// public static
this.className = 'MYAPP';
// ----------------------------------------
// public static
// can be overridden -- a new property is created for that instance
my.prototype = {
evtsrc_onopen: function ( evt ) {
log_hook('Opened');
},
evtsrc_onmessage: function ( evt ) {
log_hook('RECV < { origin: [' + evt.origin +
'] lastEventId: [' + evt.lastEventId +
'] source: [' + evt.source +
'] port: [' + evt.port + '] }');
log_hook('RECV < [' + evt.data +']');
// to double check origin
// if (evt.origin != 'http://googlecodesamples.com') {
// log_log('Origin was not http://googlecodesamples.com');
// return;
// }
// to parse JSON
// var data = JSON.parse(evt.data);
// log_hook( 'msg: ' + data.msg );
},
evtsrc_onerror: function ( evt ) {
if (evt.eventPhase == 2) { //EventSource.CLOSED
log_hook('> Connection was closed');
} else {
log_hook('Error: ' + evt.data);
}
},
evtsrc_onclose: function ( evt ) {
log_hook( 'this is not used in EventSource?' );
},
// ----------------------------------------
init: function ( url ) {
if ( ! this.classcheck() )
return MYAPPET.MYAPP_BAD_CLASS;
var evtsrc = new EVENTSOURCE;
if ( ! evtsrc )
return MYAPP.MYAPP_ERNO_NO_EVENTSOURCE;
this.setsource( evtsrc );
evtsrc.open( url, this.evtsrc_onopen, this.evtsrc_onmessage, this.evtsrc_onerror, this.evtsrc_onclose );
log_hook('Connect init(): ' + url);
return MYAPP.MYAPP_SUCCESS;
},
// ----------------------------------------
// house keeping
classcheck: function () {
if ( this.className === MYAPP.className )
return true;
throw_hook( "*** ERRRO: object's className mismatch" );
return false;
},
donothing: function () {} // no-op
};
return my; // must be last
}());
</script>
<script>
var myapp = null;
function eventsource_onload() {
// ----------------------------------------
var url = '/HTML5/cgi-bin/sse.pl';
// var url = 'servers/sse.php';
myapp = new MYAPP;
myapp.init( url );
// remember: the first getsource() is EVENTSOURCE
// the second getsource() is the actual EventSource object
var evtsrc = myapp.getsource().getsource();
// adding custom 'event name' handlers
evtsrc.addEventListener('login', function(e) {
log_hook( "EventName: login[ " + e.data + " ]" );
}, false);
evtsrc.addEventListener('server-time', function(e) {
log_hook( "EventName: server-time[ " + e.data + " ]" );
}, false);
openclose_stop();
}
function eventsource_unload() {
if ( myapp )
myapp.getsource().getsource().close();
openclose_start();
}
function openclose_start() {
log_hook( 'EventSource manually closed -- the browser will not attempt to reconnect to server...' );
var el = document.getElementById("openclose");
el.innerHTML = "Start EventSource";
el.onclick = function() { eventsource_onload(); };
}
function openclose_stop() {
var el = document.getElementById("openclose");
el.innerHTML = "Stop EventSource";
el.onclick = function() { eventsource_unload(); };
}
</script>
<!-- .................................................. -->
<!-- eventsource }}}2 -->
<!-- this document {{{2 -->
<!-- .................................................. -->
<script>
onload = onload_handler;
onbeforeunload = onbeforeunload_handler;
function onload_handler() {
debuglog_onload();
eventsource_onload();
}
function onbeforeunload_handler() {
eventsource_unload();
}
</script>
<!-- .................................................. -->
<!-- this document }}}2 -->
<!-- Javascript }}} -->
<!-- ================================================== -->
</head>
<!-- ================================================== -->
<!-- START OF BODY -->
<body>
<!-- .................................................. -->
<div id="logwindow"></div>
<!-- .................................................. -->
<button onclick="DEBUGLOG.clear()">Clear log</button>
<button id="openclose">Please Wait...</button>
<!-- .................................................. -->
</body>
</html>