-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (41 loc) · 1.1 KB
/
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 io = require('socket.io')(4000, {
cors: {
origin: ['http://localhost:3000','https://bingoonline.netlify.app'],
},
})
io.on('connection', (socket) => {
socket.on('snd-msg', (message, room) => {
// console.log(message, room)
socket.to(room).emit('res-msg', message)
})
socket.on('join-room', (room, cb) => {
let clientno = 0
if (!io.sockets.adapter.rooms.get(room)) {
clientno = 0
} else {
clientno = io.sockets.adapter.rooms.get(room).size + 1
}
if (clientno <= 2) {
socket.join(room)
cb(`joined room ${room}`, clientno)
if (clientno === 2) {
socket.to(room).emit('res-msg', -1, true)
}
} else {
cb(`room full ${room}`, clientno)
}
})
socket.on('leave-room', (room) => {
const roomName = room
const roomSockets = io.sockets.adapter.rooms.get(roomName)
if (roomSockets) {
const socketIds = Array.from(roomSockets)
socketIds.forEach((socketId) => {
const socket = io.sockets.sockets.get(socketId)
if (socket) {
socket.leave(roomName)
}
})
}
})
})