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 @@