🌽🍤 Checkpoint

./server.js:530995/152
./views/room.handlebars:530995/21
./room.js:530995/506
This commit is contained in:
Glitch (hello-express) 2020-04-09 15:14:45 +00:00
parent 0c282eaf59
commit 645e0f11e5
3 changed files with 10 additions and 2 deletions

View File

@ -36,4 +36,9 @@ function addParticipant(roomId, participantId, participantName) {
rooms[roomId] = room; rooms[roomId] = room;
} }
module.exports = {getOrCreateRoom, addParticipant} function addParticipantWS(roomId, participantId, ws) {
let room = getOrCreateRoom(roomId);
room.participants.find(p => p.participantId === participantId).ws = ws;
}
module.exports = {getOrCreateRoom, addParticipant, addParticipantWS}

View File

@ -70,10 +70,13 @@ server.listen(process.env.PORT, () => {
const wss = new WebSocket.Server({ server }); const wss = new WebSocket.Server({ server });
wss.on('connection', (ws, req) => { wss.on('connection', (ws, req) => {
let participant; let participant;
let roomId = req.url.substring(1);
ws.on('message', (message) => { ws.on('message', (message) => {
message = JSON.parse(message); message = JSON.parse(message);
if (message.type === "join") { if (message.type === "join") {
participant = message.data; participant = message.data;
rooms.addParticipantWS(roomId, participant.participantId, ws)
ws.send('Joined as ' + participant.participantName); ws.send('Joined as ' + participant.participantName);
} }
if (message.type === "buzz") { if (message.type === "buzz") {

View File

@ -24,7 +24,7 @@
</main> </main>
<script> <script>
let socket = new WebSocket(`wss://${window.location.hostname}`); let socket = new WebSocket(`wss://${window.location.hostname}/{{room}}`);
socket.onopen = function(e) { socket.onopen = function(e) {
socket.send(JSON.stringify({ socket.send(JSON.stringify({