-
Notifications
You must be signed in to change notification settings - Fork 3
/
poll_client.js
63 lines (53 loc) · 1.34 KB
/
poll_client.js
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
$(function() {
var
TIMEOUT = 3 * 1000,
PAUSE = 1 * 1000,
since = -10,
$status = $('#status'),
$messages = $('#messages'),
pollUrl = [
'http://',
window.location.hostname,
':',
8012, // @todo, read from config/default.js
'/messages'
].join('');
function poll() {
var
url = pollUrl+'?since='+since;
$status.text('Polling '+url+' ...');
$.jsonp({
url: url,
callbackParameter: 'callback',
timeout: TIMEOUT,
error: function(xOptions, status) {
if (status != 'timeout') {
$status.text('JSONP error: '+status);
} else {
$status.text('Timeout, re-connect in '+PAUSE+' ms');
}
// Wait for PAUSE ms before re-connecting
setTimeout(function() {
poll();
}, PAUSE);
},
success: function(r) {
// Remember were we left off
since = r.seq;
$status.text('Fetched '+r.messages.length+' messages, re-connect in '+PAUSE+' ms');
// Show the new messages
$.each(r.messages, function() {
$messages.prepend(
$('<li/>')
.text(this.message)
);
});
// Wait for PAUSE ms before re-connecting
setTimeout(function() {
poll();
}, PAUSE);
}
})
}
poll();
});