-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (27 loc) · 964 Bytes
/
index.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
const fs = require("fs");
const path = require("path");
const https = require("https");
const WebSocket = require("ws");
const server = https.createServer({
ca: fs.readFileSync(path.join(__dirname, "privatesuitemag_com.ca-bundle")),
cert: fs.readFileSync(path.join(__dirname, "privatesuitemag_com.crt")),
key: fs.readFileSync(path.join(__dirname, "privatesuitemag_com.key"))
});
const wss = new WebSocket.Server({ server });
const rooms = new Map();
wss.on("connection", conn => {
console.log(`New connection to room ${conn.protocol}`);
rooms.set(conn.protocol, [...(rooms.get(conn.protocol) || []), conn]);
conn.on("message", message => {
let i = 0;
for (const a of rooms.get(conn.protocol)) {
i++;
if (rooms.get(conn.protocol).indexOf(conn) === i - 1) continue;
a.send(message);
}
});
conn.on("disconnect", () => {
rooms.get(conn.protocol).splice(rooms.get(conn.protocol).indexOf(conn));
});
});
server.listen(8080);