mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:10:42 +00:00
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
This commit is contained in:
+19
-3
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user