-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (32 loc) · 1001 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
const { createServer } = require("http");
const { Server } = require("socket.io");
const httpServer = createServer();
const io = new Server(httpServer, {
cors: {
origin: "https://chathoy-client.tickflow.net",
methods: ["GET", "POST"],
},
});
httpServer.listen(3000);
let numClients = [];
let roomNumber = new Map();
io.on("connection", (socket) => {
socket.on("joinRoom", (data) => {
socket.join(data);
roomNumber.set(socket.id, data);
if (numClients[data] == undefined || numClients[data] <= 0) {
numClients[data] = 1;
} else {
numClients[data]++;
}
io.to(data).emit("numberClients", numClients[data]);
});
socket.on("send_message", (data) => {
socket.to(data.room).emit("receive_message", data);
});
socket.on("disconnect", () => {
let cnt = roomNumber.get(socket.id);
numClients[cnt]--;
io.to(cnt).emit("numberClients", numClients[cnt]);
});
});