mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 15:40:43 +00:00
error on close ws
This commit is contained in:
@@ -100,17 +100,6 @@ export function joinLobby(
|
||||
terrainLoad,
|
||||
).then((r) => r.start());
|
||||
}
|
||||
if (message.type === "error") {
|
||||
showErrorModal(
|
||||
message.error,
|
||||
message.message,
|
||||
lobbyConfig.gameID,
|
||||
lobbyConfig.clientID,
|
||||
true,
|
||||
false,
|
||||
"error_modal.connection_error",
|
||||
);
|
||||
}
|
||||
};
|
||||
transport.connect(onconnect, onmessage);
|
||||
return () => {
|
||||
@@ -326,17 +315,6 @@ export class ClientGameRunner {
|
||||
"error_modal.desync_notice",
|
||||
);
|
||||
}
|
||||
if (message.type === "error") {
|
||||
showErrorModal(
|
||||
message.error,
|
||||
message.message,
|
||||
this.lobby.gameID,
|
||||
this.lobby.clientID,
|
||||
true,
|
||||
false,
|
||||
"error_modal.connection_error",
|
||||
);
|
||||
}
|
||||
if (message.type === "turn") {
|
||||
if (!this.hasJoined) {
|
||||
this.transport.joinGame(0);
|
||||
|
||||
+1
-10
@@ -88,8 +88,7 @@ export type ServerMessage =
|
||||
| ServerStartGameMessage
|
||||
| ServerPingMessage
|
||||
| ServerDesyncMessage
|
||||
| ServerPrestartMessage
|
||||
| ServerErrorMessage;
|
||||
| ServerPrestartMessage;
|
||||
|
||||
export type ServerTurnMessage = z.infer<typeof ServerTurnMessageSchema>;
|
||||
export type ServerStartGameMessage = z.infer<
|
||||
@@ -98,7 +97,6 @@ export type ServerStartGameMessage = z.infer<
|
||||
export type ServerPingMessage = z.infer<typeof ServerPingMessageSchema>;
|
||||
export type ServerDesyncMessage = z.infer<typeof ServerDesyncSchema>;
|
||||
export type ServerPrestartMessage = z.infer<typeof ServerPrestartMessageSchema>;
|
||||
export type ServerErrorMessage = z.infer<typeof ServerErrorSchema>;
|
||||
export type ClientSendWinnerMessage = z.infer<typeof ClientSendWinnerSchema>;
|
||||
export type ClientPingMessage = z.infer<typeof ClientPingMessageSchema>;
|
||||
export type ClientIntentMessage = z.infer<typeof ClientIntentMessageSchema>;
|
||||
@@ -445,19 +443,12 @@ export const ServerDesyncSchema = z.object({
|
||||
yourHash: z.number().optional(),
|
||||
});
|
||||
|
||||
export const ServerErrorSchema = z.object({
|
||||
type: z.literal("error"),
|
||||
error: z.string(),
|
||||
message: z.string().optional(),
|
||||
});
|
||||
|
||||
export const ServerMessageSchema = z.discriminatedUnion("type", [
|
||||
ServerTurnMessageSchema,
|
||||
ServerPrestartMessageSchema,
|
||||
ServerStartGameMessageSchema,
|
||||
ServerPingMessageSchema,
|
||||
ServerDesyncSchema,
|
||||
ServerErrorSchema,
|
||||
]);
|
||||
|
||||
//
|
||||
|
||||
+10
-26
@@ -13,7 +13,6 @@ import {
|
||||
Intent,
|
||||
PlayerRecord,
|
||||
ServerDesyncSchema,
|
||||
ServerErrorMessage,
|
||||
ServerPrestartMessageSchema,
|
||||
ServerStartGameMessage,
|
||||
ServerTurnMessage,
|
||||
@@ -194,17 +193,8 @@ export class GameServer {
|
||||
this.log.error("Failed to parse client message", error, {
|
||||
clientID: client.clientID,
|
||||
});
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
type: "error",
|
||||
error,
|
||||
message,
|
||||
} satisfies ServerErrorMessage),
|
||||
);
|
||||
// Add a small delay before closing the connection to ensure the error message is received
|
||||
setTimeout(() => {
|
||||
client.ws.close(1002, "ClientMessageSchema");
|
||||
}, 100);
|
||||
client.ws.removeAllListeners();
|
||||
client.ws.close(1002, `Invalid client message: ${error}`);
|
||||
return;
|
||||
}
|
||||
const clientMsg = parsed.data;
|
||||
@@ -545,21 +535,15 @@ export class GameServer {
|
||||
clientID: client.clientID,
|
||||
persistentID: client.persistentID,
|
||||
});
|
||||
client.ws.send(
|
||||
JSON.stringify({
|
||||
type: "error",
|
||||
error: "Kicked from game (you may have been playing on another tab)",
|
||||
} satisfies ServerErrorMessage),
|
||||
client.ws.close(
|
||||
1002,
|
||||
"Kicked from game (you may have been playing on another tab)",
|
||||
);
|
||||
// Add a small delay before closing the connection to ensure the error message is received
|
||||
setTimeout(() => {
|
||||
client.ws.close(1000, "Kicked from game");
|
||||
this.activeClients = this.activeClients.filter(
|
||||
(c) => c.clientID !== clientID,
|
||||
);
|
||||
client.ws.removeAllListeners();
|
||||
this.kickedClients.add(clientID);
|
||||
}, 100);
|
||||
this.activeClients = this.activeClients.filter(
|
||||
(c) => c.clientID !== clientID,
|
||||
);
|
||||
client.ws.removeAllListeners();
|
||||
this.kickedClients.add(clientID);
|
||||
} else {
|
||||
this.log.warn(`cannot kick client, not found in game`, {
|
||||
clientID,
|
||||
|
||||
+2
-15
@@ -15,7 +15,6 @@ import {
|
||||
ClientMessageSchema,
|
||||
GameRecord,
|
||||
GameRecordSchema,
|
||||
ServerErrorMessage,
|
||||
} from "../core/Schemas";
|
||||
import { CreateGameInputSchema, GameInputSchema } from "../core/WorkerSchemas";
|
||||
import { archive, readGameRecord } from "./Archive";
|
||||
@@ -310,14 +309,8 @@ export function startWorker() {
|
||||
if (!parsed.success) {
|
||||
const error = z.prettifyError(parsed.error);
|
||||
log.warn("Error parsing client message", error);
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
type: "error",
|
||||
error: error.toString(),
|
||||
} satisfies ServerErrorMessage),
|
||||
);
|
||||
ws.removeAllListeners();
|
||||
ws.close(1002, "ClientJoinMessageSchema");
|
||||
ws.close(1002, `Failed to parse client message: ${error}`);
|
||||
return;
|
||||
}
|
||||
const clientMsg = parsed.data;
|
||||
@@ -328,14 +321,8 @@ export function startWorker() {
|
||||
} 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.removeAllListeners();
|
||||
ws.close(1002, "ClientJoinMessageSchema");
|
||||
ws.close(1002, error);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user