-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse-server.js
47 lines (43 loc) · 1.2 KB
/
sse-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
var http = require('http');
http
.createServer(function (req, res) {
var fileName = req.url;
console.log(fileName);
if (fileName === '/api') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
'Access-Control-Allow-Origin': '*'
});
res.end('data: ' + new Date() + '\n\n');
}
})
.listen(8844, '127.0.0.1');
/* http
.createServer(function (req, res) {
var fileName = req.url;
console.log(fileName);
if (fileName === '/stream') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
'Access-Control-Allow-Origin': '*'
});
res.write('retry: 10000\n');
res.write('event: connecttime\n');
res.write('data: ' + new Date() + '\n\n');
interval = setInterval(function () {
res.write('data: ' + new Date() + '\n\n');
}, 1000);
req.connection.addListener(
'close',
function () {
clearInterval(interval);
},
false
);
}
})
.listen(8844, '127.0.0.1'); */