feat(#25): Add ability to kick a user

This commit is contained in:
Josh Creek
2024-01-05 18:52:34 +00:00
parent 397aa528dc
commit 6ae45d9c6a
4 changed files with 118 additions and 2 deletions
+9
View File
@@ -66,6 +66,15 @@ export class Room {
return null;
}
getWebSocketByUserId(userId: String): WebSocket | null {
for (const [ws, u] of this.users) {
if (u.userId === userId) {
return ws;
}
}
return null;
}
broadcast(message: any) {
this.users.forEach((user, client) => {
if (client.readyState === WebSocket.OPEN) {
+11
View File
@@ -167,6 +167,17 @@ wss.on('connection', (ws: WebSocket, req) => {
if (room) {
broadcastToRoom(room.id, 'pong');
}
} else if (data.type === 'kick-user') {
console.log('kick user message received');
if (room) {
const userIdToKick = data.userId;
const ws = room.getWebSocketByUserId(userIdToKick);
console.log(userIdToKick, ws);
if (ws) {
console.log('closing ws');
ws.close();
}
}
}
});