forked from remy/eventsource-h5d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (113 loc) · 2.98 KB
/
app.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
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
var fs = require('fs'),
connect = require('connect'),
http = require('http'),
exec = require('child_process').exec,
connections = [],
history = [],
lastMessageId = 0,
uptimeTimeout = 1 * 1000,
totalRequests = 0;
setTimeout(function uptime() {
var child = exec('uptime', function (err, stdout, stderr) {
if (!err) {
var loadraw = stdout.trim().replace(/,/g, '').split(/\s+/),
load = {
15: loadraw.pop(),
5: loadraw.pop(),
1: loadraw.pop()
};
broadcast('uptime', load);
}
setTimeout(uptime, uptimeTimeout);
});
}, uptimeTimeout);
setTimeout(function time() {
broadcast('time', +new Date);
setTimeout(time, uptimeTimeout);
}, uptimeTimeout);
function removeConnection(res) {
var i = connections.indexOf(res);
if (i !== -1) {
connections.splice(i, 1);
}
}
function router(req, res, next) {
if (req.headers.accept == 'text/event-stream') {
res.writeHead(200, {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
'connection': 'keep-alive'
});
// support the polyfill
if (req.headers['x-requested-with'] == 'XMLHttpRequest') {
res.xhr = null;
}
if (req.headers['last-event-id']) {
var id = parseInt(req.headers['last-event-id']);
for (var i = 0; i < history.length; i++) {
if (history[i].id >= id) {
sendSSE(res, history[i].id, history[i].event, history[i].message);
}
}
} else {
// resets the ID
res.write('id\n\n');
}
connections.push(res);
broadcast('connections', connections.length);
req.on('close', function () {
removeConnection(res);
});
} else {
// arbitrarily redirect them away from this url
res.writeHead(302, { location: "/index.html" });
res.end();
}
}
function broadcast(event, message) {
message = JSON.stringify(message);
++lastMessageId;
history.push({
id: lastMessageId,
event: event,
message: message
});
//console.log('broadcast to %d connections', connections.length);
connections.forEach(function (res) {
sendSSE(res, lastMessageId, event, message);
});
}
function sendSSE(res, id, event, message) {
var data = '';
if (event) {
data += 'event: ' + event + '\n';
}
// blank id resets the id counter
if (id) {
data += 'id: ' + id + '\n';
} else {
data += 'id\n';
}
if (message) {
data += 'data: ' + message.split(/\n/).join('\ndata:') + '\n';
}
data += '\n'; // final part of message
res.write(data);
if (res.hasOwnProperty('xhr')) {
clearTimeout(res.xhr);
res.xhr = setTimeout(function () {
res.end();
removeConnection(res);
}, 250);
}
// console.log(data);
}
var app = connect();
app.use(function (req, res, next) {
broadcast('requests', ++totalRequests);
next();
});
app.use(connect.static(__dirname + '/public'));
app.use(router);
http.createServer(app).listen(process.env.PORT || 8000);
console.log("Listening");