/
server.js
56 lines (46 loc) · 1.7 KB
/
server.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
const http = require('http');
const msg = " - Simple SSE server - ";
const port = 5000;
// Create basic server
http.createServer(function (request, response) {
// answer only to event stream requests
if (request.headers.accept && request.headers.accept == 'text/event-stream') {
// check if the resource is what we want
// => http://domain.ext/sse
if (/sse/gim.test(request.url)) {
sendSSE(request, response);
}
}
else {
// if not just return that you are online and a string of text
response.writeHead(200);
response.write('Welcome to ' + msg + '@ :' + port);
response.end();
}
}).listen(port);
// Setup the SSE "service" :)
function sendSSE(request, response) {
// Setup headers
// For ease of use: example for enabled CORS
response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
// enabling CORS
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
});
var id = (new Date()).toLocaleTimeString();
// send first message
constructSSE(response, id, (new Date()).toLocaleTimeString());
// send message every second and a half
setInterval(function () {
constructSSE(response, id, (new Date()).toLocaleTimeString());
}, 1500);
}
// setup simple message
function constructSSE(response, id, data) {
response.write('id: ' + id + '\n');
// response.write('event: ' + 'add' + '\n');
response.write("data: " + msg + port + '\n\n');
}