-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (44 loc) · 1.35 KB
/
server.js
File metadata and controls
57 lines (44 loc) · 1.35 KB
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
57
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const port = 42069;
let messages = [];
let clients = [];
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.get('/messageStream', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
// Send the entire chat history to the new client
messages.forEach(message => {
res.write(`data: ${JSON.stringify(message)}\n\n`);
});
// Add client to the array
const client = { id: Date.now(), res };
clients.push(client);
// Remove client if disconnected
req.on('close', () => {
clients = clients.filter(c => c.id !== client.id);
res.end();
});
});
app.post("/sendMessage", (req, res) => {
const user = req.body.user;
const text = req.body.message;
const message = { user, text, timeStamp: new Date().toISOString() };
messages.push(message);
// push to all clients
clients.forEach(client => {
client.res.write(`data: ${JSON.stringify(message)}\n\n`);
});
res.status(200).json({ success: true });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});