-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
44 lines (33 loc) · 1.02 KB
/
index.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
var PORT = 1111;
var version = '1.2.2';
var NodeStatic = require('node-static');
var SSE = require('sse');
var staticServer = new NodeStatic.Server('./public');
var httpServer = require('http').createServer(function(request, response) {
request.addListener('end', function() {
var sender = request.connection.remoteAddress;
if(request.method == 'POST') {
broadcast('buzz', JSON.stringify({from: sender}));
response.writeHead(204);
response.end();
}
else
staticServer.serve(request, response);
}).resume();
});
var connections = [];
var sse = new SSE(httpServer);
sse.on('connection', function(client) {
connections.push(client);
client.req.setTimeout(0);
client.send('version', version);
client.on('close', function() {
connections.splice(connections.indexOf(client));
});
});
function broadcast(event, data) {
connections.forEach(function(c) {
c.send(event, data);
});
}
httpServer.listen(PORT);