From 5bf60961808c7945de36635487a8d3157985bcf1 Mon Sep 17 00:00:00 2001 From: "Glitch (hello-express)" Date: Thu, 9 Apr 2020 15:26:47 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=BD=F0=9F=91=AB=20Checkpoint=20./serve?= =?UTF-8?q?r.js:530995/100=20./room.js:530995/323=20./views/room.handlebar?= =?UTF-8?q?s:530995/140?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- room.js | 14 +++++++++++++- server.js | 2 +- views/room.handlebars | 7 +++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/room.js b/room.js index b0b7959..f7e9536 100644 --- a/room.js +++ b/room.js @@ -41,4 +41,16 @@ function addParticipantWS(roomId, participantId, ws) { room.participants.find(p => p.participantId === participantId).ws = ws; } -module.exports = {getOrCreateRoom, addParticipant, addParticipantWS} \ No newline at end of file +function buzz(roomId, participant) { + let room = getOrCreateRoom(roomId); + room.participants.forEach(p => { + if (p.ws) { + p.ws.send(JSON.stringify({ + type: "buzz", + participant: participant.participantName + })); + } + }) +} + +module.exports = {getOrCreateRoom, addParticipant, addParticipantWS, buzz} \ No newline at end of file diff --git a/server.js b/server.js index 25fced3..552499c 100644 --- a/server.js +++ b/server.js @@ -80,8 +80,8 @@ wss.on('connection', (ws, req) => { ws.send('Joined as ' + participant.participantName); } if (message.type === "buzz") { - // TODO: Handle Buzzer console.log(`${participant.participantName} buzzed!`) + rooms.buzz(roomId, participant); } }); diff --git a/views/room.handlebars b/views/room.handlebars index a5be76e..4f6a753 100644 --- a/views/room.handlebars +++ b/views/room.handlebars @@ -36,8 +36,11 @@ })); }; - socket.onmessage = function(event) { - console.log(event); + socket.onmessage = function(msg) { + msg = JSON.parse(msg); + if (msg.type === "buzz") { + alert(`${msg.participant} buzzed`); + } }; socket.onerror = function(error) {