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) {