feat(*): Enhance ws server to use classes

This commit is contained in:
Josh Creek
2023-07-09 21:32:47 +00:00
parent 51d6bbff98
commit e745613a1e
+90 -41
View File
@@ -1,12 +1,14 @@
import express from 'express'; import express from 'express';
import { WebSocketServer, WebSocket } from 'ws'; import { WebSocketServer, WebSocket } from 'ws';
import { Room } from './classes/Room.js';
import { User } from './classes/User.js';
import { JoinRoomMessage } from './classes/messages/JoinRoomMessage.js';
import { ChangeEstimateMessage } from './classes/messages/ChangeEstimateMessage.js';
import { SelectEstimateMessage } from './classes/messages/SelectEstimateMessage.js';
const app = express(); const app = express();
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
const clients = new Map();
const estimates = new Map();
function onSocketPreError(e: Error) { function onSocketPreError(e: Error) {
console.log(e); console.log(e);
} }
@@ -17,13 +19,14 @@ function onSocketPostError(e: Error) {
console.log(`Attempting to run server on port ${port}`); console.log(`Attempting to run server on port ${port}`);
const s = app.listen(port, () => { const server = app.listen(port, () => {
console.log(`Listening on port ${port}`); console.log(`Listening on port ${port}`);
}); });
const wss = new WebSocketServer({ noServer: true }); const wss = new WebSocketServer({ noServer: true });
const rooms = new Set<Room>();
s.on('upgrade', (req, socket, head) => { server.on('upgrade', (req, socket, head) => {
socket.on('error', onSocketPreError); socket.on('error', onSocketPreError);
// perform auth // perform auth
@@ -39,67 +42,113 @@ s.on('upgrade', (req, socket, head) => {
}); });
}); });
wss.on('connection', (ws, req) => { wss.on('connection', (ws: WebSocket, req) => {
ws.on('error', onSocketPostError); let room: Room | null = null;
let roomId: string; ws.on('error', onSocketPostError);
ws.on('message', (message) => { ws.on('message', (message) => {
const data = JSON.parse(message.toString()); const data = JSON.parse(message.toString());
if (data.type === 'join-room') { if (data.type === 'join-room') {
roomId = data.roomId; const joinRoomMessage: JoinRoomMessage = data as JoinRoomMessage;
clients.set(ws, roomId); const { roomId, userId, name } = joinRoomMessage;
broadcast(roomId, { type: 'user-joined', message: 'A new user has joined the room' }); room = getRoomById(roomId);
} else if (data.type === 'select-estimate') {
const { user, estimate } = data;
if (!estimates.has(roomId)) { // Create room if it doesn't already exist
estimates.set(roomId, new Map()); if (!room) {
room = new Room(roomId);
rooms.add(room);
} }
estimates.get(roomId).set(user, estimate);
broadcast(roomId, { type: 'estimate-selected', user, estimate }); // Create the user in the room
const user = new User(userId, name);
room.addUser(ws, user);
// Check if all users have selected an estimate broadcastToRoom(roomId, { type: 'user-joined', message: `${name} has joined the room` });
if (estimates.get(roomId).size === clientsInRoom(roomId).length) { } else if (data.type === 'select-estimate') {
broadcast(roomId, { type: 'estimation-closed', message: 'Estimation closed' }); const selectEstimateMessage: SelectEstimateMessage = data as SelectEstimateMessage;
const { userId, estimate } = selectEstimateMessage;
if (room) {
const user = room.getUserByWebSocket(ws);
if (user) {
user.estimate = estimate;
broadcastToRoom(room.id, { type: 'estimate-selected', userId, estimate });
const allEstimates = room.getAllEstimates();
if (allEstimates.size === room.getUsers().length) {
broadcastToRoom(room.id, { type: 'estimation-closed', message: 'Estimation closed' });
}
}
} }
} else if (data.type === 'change-estimate') { } else if (data.type === 'change-estimate') {
const { user, estimate } = data; const changeEstimateMessage: ChangeEstimateMessage = data as ChangeEstimateMessage;
const { userId, estimate } = changeEstimateMessage;
if (estimates.has(roomId)) { if (room) {
estimates.get(roomId).set(user, estimate); const user = room.getUserByWebSocket(ws);
broadcast(roomId, { type: 'estimate-changed', user, estimate }); if (user) {
user.estimate = estimate;
broadcastToRoom(room.id, { type: 'estimate-changed', userId, estimate });
}
const allEstimates = room.getAllEstimates();
if (allEstimates.size === room.getUsers().length) {
broadcastToRoom(room.id, { type: 'estimation-closed', message: 'Estimation closed' });
}
}
} else if (data.type === 'restart-estimation') {
if (room) {
room.clearEstimates();
broadcastToRoom(room.id, { type: 'estimation-restarted', message: 'Estimation restarted' });
} }
} }
}); });
ws.on('close', () => { ws.on('close', () => {
const user = clients.get(ws); if (room) {
clients.delete(ws); const user = room.getUserByWebSocket(ws);
if (user) {
room.removeUser(ws);
if (estimates.has(roomId)) { if (room.getUsers().length === 0) {
estimates.get(roomId).delete(user); rooms.delete(room);
broadcastToRoom(room.id, {
type: 'room-deleted',
message: 'Room deleted'
});
}
if (estimates.get(roomId).size === 0) { broadcastToRoom(room.id, {
estimates.delete(roomId); type: 'user-left',
broadcast(roomId, { type: 'estimation-started', message: 'Estimation started again' }); message: `${user.userId} has left the room`
});
} }
} }
broadcast(roomId, { type: 'user-left', message: `${user} has left the room` });
}); });
}); });
function broadcast(roomId: string, message: any) { function broadcastToRoom(roomId: string, message: any) {
wss.clients.forEach((client) => { const room = getRoomById(roomId);
if (clients.get(client) === roomId && client.readyState === WebSocket.OPEN) { if (room) {
client.send(JSON.stringify(message)); room.broadcast(message);
} }
});
} }
function clientsInRoom(roomId: string) { function getUsersInRoom(roomId: string) {
return [...clients.values()].filter((id) => id === roomId); const room = getRoomById(roomId);
if (room) {
return room.getUsers();
}
return [];
}
function getRoomById(roomId: string) {
for (const room of rooms) {
if (room.id === roomId) {
return room;
}
}
return null;
} }