-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
49 lines (39 loc) · 1.2 KB
/
server.ts
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
import dotenv from "dotenv";
import WebSocket from "ws";
dotenv.config();
const port = Number(process.env.PORT);
const serverOptions = {
port,
clientTracking: true,
host: process.env.HOST,
};
const startServer = () => {
const server = new WebSocket.Server(serverOptions);
// A JavaScript Set is a collection of unique values.
// Each value can only occur once in a Set.
// The values can be of any type, primitive values or objects.
const clients: Set<WebSocket> = new Set();
console.log(`Broadcast server started on ws://localhost:${port}`);
server.on("connection", (ws) => {
console.log("New client connected.");
clients.add(ws);
ws.on("message", (message) => {
console.log(`Received: ${message}`);
clients.forEach((client) => {
// checks if The connection is open and ready to communicate.
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
ws.on("close", () => {
console.log("Client disconnected");
clients.delete(ws);
});
ws.on("error", (error) => {
console.error(`Client error ${error.message}`);
clients.delete(ws);
});
});
};
export default startServer;