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
| const { createReadStream, createWriteStream } = require("fs")
const { createServer } = require("http")
const sendInterval = 2000
createServer((req, res) => {
if (req.url === "/") {
return createReadStream("./index.html").pipe(res)
}
if (req.url.indexOf("/chat") == 0) {
res.writeHead(200, {
"Content-Type": "text/event-stream",
Connection: "keep-alive",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
})
setInterval(function () {
writeServerSendEvent(res, new Date().toLocaleTimeString())
}, sendInterval)
writeServerSendEvent(res, new Date().toLocaleTimeString())
}
}).listen(3011, () => {
console.log("listening on http://localhost:3011")
})
function writeServerSendEvent(res, data) {
res.write("data: " + data + "\n\n")
}
|