🌽🍤 Checkpoint
./server.js:530995/152 ./views/room.handlebars:530995/21 ./room.js:530995/506
This commit is contained in:
parent
0c282eaf59
commit
645e0f11e5
7
room.js
7
room.js
@ -36,4 +36,9 @@ function addParticipant(roomId, participantId, participantName) {
|
||||
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}
|
@ -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") {
|
||||
|
@ -24,7 +24,7 @@
|
||||
</main>
|
||||
|
||||
<script>
|
||||
let socket = new WebSocket(`wss://${window.location.hostname}`);
|
||||
let socket = new WebSocket(`wss://${window.location.hostname}/{{room}}`);
|
||||
|
||||
socket.onopen = function(e) {
|
||||
socket.send(JSON.stringify({
|
||||
|
Loading…
Reference in New Issue
Block a user