💽👫 Checkpoint

./server.js:530995/100
./room.js:530995/323
./views/room.handlebars:530995/140
This commit is contained in:
Glitch (hello-express) 2020-04-09 15:26:47 +00:00
parent 645e0f11e5
commit 5bf6096180
3 changed files with 19 additions and 4 deletions

14
room.js
View File

@ -41,4 +41,16 @@ function addParticipantWS(roomId, participantId, ws) {
room.participants.find(p => p.participantId === participantId).ws = ws; room.participants.find(p => p.participantId === participantId).ws = ws;
} }
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS} 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}

View File

@ -80,8 +80,8 @@ wss.on('connection', (ws, req) => {
ws.send('Joined as ' + participant.participantName); ws.send('Joined as ' + participant.participantName);
} }
if (message.type === "buzz") { if (message.type === "buzz") {
// TODO: Handle Buzzer
console.log(`${participant.participantName} buzzed!`) console.log(`${participant.participantName} buzzed!`)
rooms.buzz(roomId, participant);
} }
}); });

View File

@ -36,8 +36,11 @@
})); }));
}; };
socket.onmessage = function(event) { socket.onmessage = function(msg) {
console.log(event); msg = JSON.parse(msg);
if (msg.type === "buzz") {
alert(`${msg.participant} buzzed`);
}
}; };
socket.onerror = function(error) { socket.onerror = function(error) {