-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (33 loc) · 1.21 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
const net = require("node:net");
const PORT = 4020;
const HOST = "127.0.0.1";
const server = net.createServer();
// an array of client sockets
const clients = [];
server.on("connection", (socket) => {
console.log("A new connection to the server!");
const clientId = clients.length + 1;
// Broadcasting a message to everyone when someone enters the chat room
clients.map((client) => {
client.socket.write(`User ${clientId} joined!`);
});
socket.write(`id-${clientId}`);
socket.on("data", (data) => {
const dataString = data.toString("utf-8");
const id = dataString.substring(0, dataString.indexOf("-"));
const message = dataString.substring(dataString.indexOf("-message-") + 9);
clients.map((client) => {
client.socket.write(`> User ${id}: ${message}`);
});
});
// Broadcasting a message to everyone when someone leaves the chat room
socket.on("end", () => {
clients.map((client) => {
client.socket.write(`User ${clientId} left!`);
});
});
clients.push({ id: clientId.toString(), socket });
});
server.listen(PORT, HOST, () => {
console.log("opened server on", server.address());
});