From 9dcceefc3391e07130579642ece824beb522d1de Mon Sep 17 00:00:00 2001 From: Scott Anderson <662325+scottanderson@users.noreply.github.com> Date: Sat, 28 Jun 2025 15:29:25 -0400 Subject: [PATCH] Graceful handling of ping before join (#1295) ## Description: Graceful handling of ping before join. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced - [x] I understand that submitting code with bugs that could have been caught through manual testing blocks releases and new features for all contributors --- src/server/Worker.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/server/Worker.ts b/src/server/Worker.ts index aedea05c7..8fc67160d 100644 --- a/src/server/Worker.ts +++ b/src/server/Worker.ts @@ -11,7 +11,7 @@ import { getServerConfigFromServer } from "../core/configuration/ConfigLoader"; import { COSMETICS } from "../core/CosmeticSchemas"; import { GameType } from "../core/game/Game"; import { - ClientJoinMessageSchema, + ClientMessageSchema, GameRecord, GameRecordSchema, ServerErrorMessage, @@ -299,12 +299,12 @@ export function startWorker() { try { // Parse and handle client messages - const parsed = ClientJoinMessageSchema.safeParse( + const parsed = ClientMessageSchema.safeParse( JSON.parse(message.toString()), ); if (!parsed.success) { const error = z.prettifyError(parsed.error); - log.warn("Error parsing join message client", error); + log.warn("Error parsing client message", error); ws.send( JSON.stringify({ type: "error", @@ -316,6 +316,22 @@ export function startWorker() { } const clientMsg = parsed.data; + if (clientMsg.type === "ping") { + // Ignore ping + return; + } else if (clientMsg.type !== "join") { + const error = `Invalid message before join: ${JSON.stringify(clientMsg)}`; + log.warn(error); + ws.send( + JSON.stringify({ + type: "error", + error, + } satisfies ServerErrorMessage), + ); + ws.close(1002, "ClientJoinMessageSchema"); + return; + } + // Verify this worker should handle this game const expectedWorkerId = config.workerIndex(clientMsg.gameID); if (expectedWorkerId !== workerId) {