From 814a00c50422d62a004ec69819cde658f3df4cc6 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Wed, 23 Jul 2025 09:57:27 -0700 Subject: [PATCH] better websocket handling --- src/server/GameServer.ts | 3 ++- src/server/Worker.ts | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/GameServer.ts b/src/server/GameServer.ts index e8480e7d0..6b5cf9220 100644 --- a/src/server/GameServer.ts +++ b/src/server/GameServer.ts @@ -111,6 +111,7 @@ export class GameServer { this.log.warn(`cannot add client, already kicked`, { clientID: client.clientID, }); + client.ws.close(1002, "Kicked from game"); return; } this.log.info("client (re)joining game", { @@ -174,6 +175,7 @@ export class GameServer { client.lastPing = existing.lastPing; existing.ws.removeAllListeners(); + existing.ws.close(1000); this.activeClients = this.activeClients.filter((c) => c !== existing); } @@ -183,7 +185,6 @@ export class GameServer { this.allClients.set(client.clientID, client); - client.ws.removeAllListeners(); client.ws.on( "message", gatekeeper.wsHandler(client.ip, async (message: string) => { diff --git a/src/server/Worker.ts b/src/server/Worker.ts index 96efde652..4375cc108 100644 --- a/src/server/Worker.ts +++ b/src/server/Worker.ts @@ -442,6 +442,8 @@ export function startWorker() { clientMsg.pattern, ); + // Remove all listeners because we are passing ws ownership to the GameServer. + ws.removeAllListeners(); const wasFound = gm.addClient( client, clientMsg.gameID, @@ -452,7 +454,8 @@ export function startWorker() { log.info( `game ${clientMsg.gameID} not found on worker ${workerId}`, ); - // Handle game not found case + ws.close(1002, "Game not found"); + return; } // Handle other message types