Changed from lastping to ws.close

This commit is contained in:
aqw42
2025-05-30 23:11:20 +02:00
parent 5eed7db4e3
commit b933b3d014
+4 -22
View File
@@ -235,10 +235,12 @@ export class GameServer {
this.activeClients = this.activeClients.filter(
(c) => c.clientID !== client.clientID,
);
this.markClientDisconnected(client, true);
});
client.ws.on("error", (error: Error) => {
if ((error as any).code === "WS_ERR_UNEXPECTED_RSV_1") {
client.ws.close(1002);
this.markClientDisconnected(client, true);
}
});
@@ -359,7 +361,6 @@ export class GameServer {
this.intents = [];
this.handleSynchronization();
this.checkDisconnectedStatus();
let msg = "";
try {
@@ -444,6 +445,7 @@ export class GameServer {
});
if (client.ws.readyState === WebSocket.OPEN) {
client.ws.close(1000, "no heartbeats received, closing connection");
this.markClientDisconnected(client, true);
}
} else {
alive.push(client);
@@ -529,6 +531,7 @@ export class GameServer {
persistentID: client.persistentID,
});
client.ws.close(1000, "Kicked from game");
this.markClientDisconnected(client, true);
this.activeClients = this.activeClients.filter(
(c) => c.clientID !== clientID,
);
@@ -540,27 +543,6 @@ export class GameServer {
}
}
private checkDisconnectedStatus() {
if (this.turns.length % 5 !== 0) {
return;
}
const now = Date.now();
for (const [clientID, client] of this.allClients) {
if (
client.isDisconnected === false &&
now - client.lastPing > this.disconnectedTimeout
) {
this.markClientDisconnected(client, true);
} else if (
client.isDisconnected &&
now - client.lastPing < this.disconnectedTimeout
) {
this.markClientDisconnected(client, false);
}
}
}
private markClientDisconnected(client: Client, isDisconnected: boolean) {
client.isDisconnected = isDisconnected;
this.addIntent({