From 067859e458a58bb5622071dfd33b018dc051b316 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Sat, 5 Jul 2025 16:17:23 -0600 Subject: [PATCH] fix: Get server URL from runtime config instead of build-time env vars - Client now fetches server URL from /api/config endpoint at runtime - Resolves issue with localhost:2567 hardcoded in production builds - Client will use snatchGameServer.interno.com in production - Lazy initialization of Colyseus client with proper error handling --- client/src/services/gameClient.ts | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/client/src/services/gameClient.ts b/client/src/services/gameClient.ts index 76ab22e..d259cb8 100644 --- a/client/src/services/gameClient.ts +++ b/client/src/services/gameClient.ts @@ -4,7 +4,7 @@ import type { GameRoomOptions } from '../types' import { logger } from './logger' export class GameClient { - public client: Client + public client: Client | null = null public room: Room | null = null // Current state @@ -19,13 +19,33 @@ export class GameClient { private onRoundChangedCallbacks: ((data: any) => void)[] = [] constructor() { - const serverUrl = import.meta.env.VITE_SERVER_URL || 'ws://localhost:2567' - this.client = new Client(serverUrl) - logger.info('Game client initialized with server:', serverUrl) + // Client will be initialized when needed + } + + private async ensureClientInitialized() { + if (this.client) return + + try { + // Fetch runtime configuration from our Express server + const configResponse = await fetch('/api/config') + const config = await configResponse.json() + const serverUrl = config.serverUrl || 'ws://localhost:2567' + + this.client = new Client(serverUrl) + logger.info('Game client initialized with server:', serverUrl) + } catch (error) { + // Fallback to default if config fetch fails + const defaultUrl = 'ws://localhost:2567' + this.client = new Client(defaultUrl) + logger.warn('Failed to fetch config, using default:', defaultUrl) + } } async joinGame(playerName: string, gameMode: string = 'classic'): Promise> { try { + await this.ensureClientInitialized() + if (!this.client) throw new Error('Failed to initialize game client') + logger.info('Attempting to join game room...') const options: GameRoomOptions = {