🐋🏥 Checkpoint
./room.js:530995/285 ./server.js:530995/832
This commit is contained in:
parent
0f24a75183
commit
b337088cb6
15
room.js
15
room.js
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
const rooms = {};
|
const rooms = {};
|
||||||
|
|
||||||
export function getOrCreateRoom(roomId) {
|
function getOrCreateRoom(roomId) {
|
||||||
let room = rooms[roomId];
|
let room = rooms[roomId];
|
||||||
|
|
||||||
if (!room) {
|
if (!room) {
|
||||||
@ -15,7 +15,14 @@ export function getOrCreateRoom(roomId) {
|
|||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addParticipant(roomId, participantId, participantName) {
|
||||||
export addParticipant(roomId, participantId, participantName) {
|
let room = getOrCreateRoom(roomId);
|
||||||
|
|
||||||
}
|
room.participants.push({
|
||||||
|
participantId,
|
||||||
|
participantName,
|
||||||
|
});
|
||||||
|
|
||||||
|
rooms[roomId] = room;
|
||||||
|
}
|
||||||
|
|
||||||
|
17
server.js
17
server.js
@ -3,6 +3,8 @@ const exphbs = require('express-handlebars');
|
|||||||
const bodyParser = require('body-parser')
|
const bodyParser = require('body-parser')
|
||||||
const Fingerprint = require('express-fingerprint')
|
const Fingerprint = require('express-fingerprint')
|
||||||
|
|
||||||
|
const rooms = require('./room');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(Fingerprint({
|
app.use(Fingerprint({
|
||||||
@ -24,10 +26,23 @@ app.set('view engine', 'handlebars');
|
|||||||
app.use(express.static("public"));
|
app.use(express.static("public"));
|
||||||
|
|
||||||
app.get("/:roomId/join", (request, response) => {
|
app.get("/:roomId/join", (request, response) => {
|
||||||
response.render('join', {layout: false, room: request.params.roomId});
|
|
||||||
|
let room = rooms.getOrCreateRoom(request.params.roomId);
|
||||||
|
let participant = room.participants.find(p => p.participantId === request.fingerprint.hash);
|
||||||
|
|
||||||
|
if (participant) {
|
||||||
|
response.render('room', {
|
||||||
|
layout: false,
|
||||||
|
room: request.params.roomId,
|
||||||
|
name: participant.participantName
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
response.render('join', {layout: false, room: request.params.roomId});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/:roomId/join", (request, response) => {
|
app.post("/:roomId/join", (request, response) => {
|
||||||
|
rooms.addParticipant(request.params.roomId, request.fingerprint.hash, request.body.name);
|
||||||
response.render('room', {
|
response.render('room', {
|
||||||
layout: false,
|
layout: false,
|
||||||
room: request.params.roomId,
|
room: request.params.roomId,
|
||||||
|
Loading…
Reference in New Issue
Block a user