-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.js
42 lines (28 loc) · 909 Bytes
/
sockets.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
let readyPlayerCount=0;
function listen(io){
const pongNameSpace=io.of('/pong')
pongNameSpace.on('connection',(socket)=>{
console.log('A user connected',socket.id)
let room='room'+ Math.floor(readyPlayerCount/2)
socket.on('ready',()=>{
socket.join(room)
console.log(`Player Ready.. ${socket.id} and room joined :${room}`)
readyPlayerCount++;
if(readyPlayerCount%2===0){
pongNameSpace.in(room).emit('startGame',socket.id)
console.log('Game Started')
}
})
socket.on('paddleMove',(paddleData)=>{
socket.to(room).emit('paddleMove',paddleData)
})
socket.on('ballMove',(ballData)=>{
socket.to(room).emit('ballMove',ballData)
})
socket.on('disconnect',(reason)=>{
console.log(`Client ${socket.id} disconnected`)
socket.leave(room)
})
})
}
module.exports={listen}