From 36abb68df9fa1abbeebbeff310b4e0f4dae34351 Mon Sep 17 00:00:00 2001 From: "duyanhnguyen2001@gmail.com" Date: Sun, 3 Jul 2022 10:39:03 +0700 Subject: [PATCH] room --- src/routers/ws/playRoutes.ts | 40 +++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/routers/ws/playRoutes.ts b/src/routers/ws/playRoutes.ts index 0612ed0..c45e224 100644 --- a/src/routers/ws/playRoutes.ts +++ b/src/routers/ws/playRoutes.ts @@ -51,11 +51,31 @@ const getPlayRoutes = (): expressWs.Router => { return; } - const player:Player = { + const player: Player = { id: playerId, connection: ws } - currentRoom.set(playerId,player); + currentRoom.set(playerId, player); + + // + currentRoom.forEach((rplayer) => { + if (rplayer.id === player.id) { + return; + } + player.connection.send(JSON.stringify( + { + method: 'SUBSCRIBE', + player: rplayer.id + } + )); + rplayer.connection.send(JSON.stringify( + { + method: 'SUBSCRIBE', + player: player.id + } + )); + }); + // ws.on('message', (data) => { const message = JSON.parse(data.toString()); @@ -64,7 +84,12 @@ const getPlayRoutes = (): expressWs.Router => { if (rplayer.id === player.id) { return; } - rplayer.connection.send(JSON.stringify({player: player.id, messsage: message})); + rplayer.connection.send(JSON.stringify( + { + method: 'MESSAGE', + player: player.id, message: message + } + )); }); }); @@ -74,6 +99,15 @@ const getPlayRoutes = (): expressWs.Router => { if (currentRoom.size) { roomList.set(roomId, currentRoom); + + currentRoom.forEach((rplayer) => { + rplayer.connection.send(JSON.stringify( + { + method: 'UNSUBSCRIBE', + player: player.id + } + )); + }); } else { roomList.delete(roomId) }