-
Notifications
You must be signed in to change notification settings - Fork 502
/
long-polling.html
83 lines (69 loc) · 1.91 KB
/
long-polling.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>iComet long-polling demo</title>
</head>
<body>
<pre style="height:200px; width: 600px; border: 1px solid #999; overflow: auto; background: #eee; padding: 8px;">
## How to Use?
1. start icomet-server
2. assuming you are running nginx listening on port 8080, if not,
you will have to modify "8080" in this file to the exact port number,
configure your nginx.conf, add the lines below and restart:
location ~ ^/icomet/.* {
rewrite ^/icomet/(.*) /$1 break;
proxy_buffering off;
proxy_read_timeout 60;
proxy_connect_timeout 60;
proxy_pass http://127.0.0.1:8100;
}
3. navigate your web browser to http://127.0.0.1:8080/web/long-polling.html
or http://127.0.0.1:8080/web/forever-iframe.html
4. open a command line terminal, execute
curl "http://127.0.0.1:8000/pub?cname=a&content=hello"
</pre>
<h3>Server push messages:</h3>
<div id="output"></div>
<script>
var count = 0;
var next_seq = 0;
var last_script = null;
function sub(seq){
// setTimeout to get rid of loading status in browser status bar
setTimeout(function(){
next_seq = seq | 0;
var body = document.getElementsByTagName('body')[0];
var script = document.createElement('script');
script.src = 'http://127.0.0.1:8080/icomet/poll?cname=a&cb=icomet_cb&seq=' + next_seq;
if(last_script){
body.removeChild(last_script);
}
body.appendChild(script);
last_script = script;
}, 0);
}
function icomet_cb(msg_data){
if(msg_data instanceof Array){
//
}else{
msg_data = [msg_data];
}
for(var k in msg_data){
var msg = msg_data[k];
if(msg.type != 'data'){
continue;
}
next_seq = parseInt(msg.seq) + 1;
count ++;
var output = document.getElementById('output');
var time = '' + (new Date());
output.innerHTML += count + '. ' + time + ': ' + msg.content + '<br/>';
}
// start next polling
sub(next_seq);
}
sub();
</script>
</body>
</html>