-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
41 lines (33 loc) · 1.08 KB
/
Copy pathmain.js
File metadata and controls
41 lines (33 loc) · 1.08 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
import net from 'node:net'
const allClients = {};
let countUser = 0;
const server = net.createServer((socket) => {
// connection msg on server
console.log(`Connected-to:: ${socket.remoteAddress}`);
countUser++;
const uniqueUser = `user${countUser}`;
allClients[uniqueUser] = socket; // saving socket on the allClients object
socket.write(`You assigned username: ${uniqueUser}`);
// sending ack to client
socket.on("data", (data) => {
console.log(`Msg from ${uniqueUser}: ${data.toString()}`);
Object.keys(allClients).forEach((key) => {
if (data.toString().includes(key)) {
if (key !== uniqueUser) {
allClients[key].write(String(data).replace(key, ""));
}
}
})
})
socket.on("end", () => {
socket.write("Ok Data received.")
console.log("data ended!");
});
});
process.stdin;
process.stdin.on("data", (data) => {
Object.keys(allClients).forEach((key) => {
allClients[key].write(data);
})
})
server.listen(3000, '0.0.0.0')