-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSSE-server.js
More file actions
41 lines (36 loc) · 984 Bytes
/
SSE-server.js
File metadata and controls
41 lines (36 loc) · 984 Bytes
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
var http = require("http");
const getData = ()=>{
return JSON.stringify([{
id:'data-0-1',
value: parseFloat(Math.random().toFixed(2))
},
{
id:'data-0-100',
value: Math.floor(Math.random() * 1000)
},
{
id:'data-bool',
value:Math.round(Math.random())
}])
}
http.createServer(function (req, res) {
var fileName = "." + req.url;
if (fileName === "./stream") {
res.writeHead(200, {
"Content-Type":"text/event-stream",
"Cache-Control":"no-cache",
"Connection":"keep-alive",
"Access-Control-Allow-Origin": '*',
});
res.write("retry: 10000\n");
res.write("event: connecttime\n");
res.write("data: " + (getData()) + "\n\n");
res.write("data: " + (getData()) + "\n\n");
interval = setInterval(function () {
res.write("data: " + (getData()) + "\n\n");
}, 1000);
req.addListener("close", function () {
clearInterval(interval);
}, false);
}
}).listen(8844, "127.0.0.1");