From 645e0f11e52c6ad118e51a5974571580334c97c4 Mon Sep 17 00:00:00 2001 From: "Glitch (hello-express)" Date: Thu, 9 Apr 2020 15:14:45 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=BD=F0=9F=8D=A4=20Checkpoint=20./serve?= =?UTF-8?q?r.js:530995/152=20./views/room.handlebars:530995/21=20./room.js?= =?UTF-8?q?:530995/506?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- room.js | 7 ++++++- server.js | 3 +++ views/room.handlebars | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/room.js b/room.js index a34878a..b0b7959 100644 --- a/room.js +++ b/room.js @@ -36,4 +36,9 @@ function addParticipant(roomId, participantId, participantName) { rooms[roomId] = room; } -module.exports = {getOrCreateRoom, addParticipant} \ No newline at end of file +function addParticipantWS(roomId, participantId, ws) { + let room = getOrCreateRoom(roomId); + room.participants.find(p => p.participantId === participantId).ws = ws; +} + +module.exports = {getOrCreateRoom, addParticipant, addParticipantWS} \ No newline at end of file diff --git a/server.js b/server.js index db6129d..25fced3 100644 --- a/server.js +++ b/server.js @@ -70,10 +70,13 @@ server.listen(process.env.PORT, () => { const wss = new WebSocket.Server({ server }); wss.on('connection', (ws, req) => { let participant; + let roomId = req.url.substring(1); + ws.on('message', (message) => { message = JSON.parse(message); if (message.type === "join") { participant = message.data; + rooms.addParticipantWS(roomId, participant.participantId, ws) ws.send('Joined as ' + participant.participantName); } if (message.type === "buzz") { diff --git a/views/room.handlebars b/views/room.handlebars index 1bc1fbc..a5be76e 100644 --- a/views/room.handlebars +++ b/views/room.handlebars @@ -24,7 +24,7 @@