fix join private lobby websocket reconnect bug

This commit is contained in:
Evan
2024-12-08 13:17:23 -08:00
parent a01b347bbf
commit 323e30b875
3 changed files with 38 additions and 25 deletions
+2 -2
View File
@@ -214,9 +214,9 @@
* record game difficulty DONE 12/7/2024
* standardize game ids DONE 12/7/2024
* bufix: mini map doesn't load in time DONE 12/7/2024
* bugfix: private game host game doesn't start
* record game winner
* bugfix: private game host game doesn't start DONE 12/8/2024
* record commit hash of game
* record game winner
* store metadata in BigQuery
* replay stored games
* max price for units
+33 -20
View File
@@ -123,16 +123,17 @@ export class Transport {
private startPing() {
if (this.isLocal || this.pingInterval) return;
this.pingInterval = window.setInterval(() => {
if (this.socket != null && this.socket.readyState === WebSocket.OPEN) {
this.sendMsg(JSON.stringify(ClientPingMessageSchema.parse({
type: 'ping',
clientID: this.clientID,
gameID: this.gameID,
})))
}
}, 10000);
if (this.pingInterval == null) {
this.pingInterval = window.setInterval(() => {
if (this.socket != null && this.socket.readyState === WebSocket.OPEN) {
this.sendMsg(JSON.stringify(ClientPingMessageSchema.parse({
type: 'ping',
clientID: this.clientID,
gameID: this.gameID,
})))
}
}, 10000);
}
}
private stopPing() {
@@ -157,12 +158,10 @@ export class Transport {
private connectRemote(onconnect: () => void, onmessage: (message: ServerMessage) => void) {
this.startPing()
const isFirstConnect = this.socket == null
if (isFirstConnect) {
const wsHost = process.env.WEBSOCKET_URL || window.location.host;
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
this.socket = new WebSocket(`${wsProtocol}//${wsHost}`)
}
this.maybeKillSocket()
const wsHost = process.env.WEBSOCKET_URL || window.location.host;
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
this.socket = new WebSocket(`${wsProtocol}//${wsHost}`)
this.onconnect = onconnect
this.onmessage = onmessage
this.socket.onopen = () => {
@@ -187,10 +186,6 @@ export class Transport {
this.connect(onconnect, onmessage)
}
};
if (!isFirstConnect) {
// Socket has already been opened, so simulate new connection.
onconnect()
}
}
joinGame(numTurns: number) {
@@ -368,4 +363,22 @@ export class Transport {
}
}
}
private maybeKillSocket(): void {
if (this.socket == null) {
return
}
// Remove all event listeners
this.socket.onmessage = null;
this.socket.onopen = null;
this.socket.onclose = null;
this.socket.onerror = null;
// Close the connection if it's still open
if (this.socket.readyState === WebSocket.OPEN) {
this.socket.close();
}
this.socket = null
}
}
+3 -3
View File
@@ -1,4 +1,4 @@
import { ClientMessage, ClientMessageSchema, GameConfig, GameRecordSchema, Intent, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { ClientMessage, ClientMessageSchema, GameConfig, GameRecordSchema, Intent, ServerPingMessageSchema, ServerStartGameMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { Config } from "../core/configuration/Config";
import { Client } from "./Client";
import WebSocket from 'ws';
@@ -35,7 +35,7 @@ export class GameServer {
private config: Config,
private gameConfig: GameConfig,
) { }
) {}
public updateGameConfig(gameConfig: GameConfig): void {
if (gameConfig.gameMap != null) {
@@ -103,11 +103,11 @@ export class GameServer {
this._hasStarted = true
this._startTime = Date.now()
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs());
this.clients.forEach(c => {
console.log(`game ${this.id} sending start message to ${c.id}`)
this.sendStartGameMsg(c.ws, 0)
})
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs());
}
private addIntent(intent: Intent) {