diff --git a/Caddyfile b/Caddyfile index fa792cd..4e2cc46 100644 --- a/Caddyfile +++ b/Caddyfile @@ -1,3 +1,10 @@ mud.iamtalon.me { + # For all WebSocket requests to /mud-ws, proxy to the WebSocket server on port 3001 + @websocket { + path /mud-ws* + } + reverse_proxy @websocket svelte-mud:3001 + + # For all other requests, proxy to the SvelteKit app on port 3000 reverse_proxy svelte-mud:3000 } diff --git a/docker-compose.yml b/docker-compose.yml index 93e4bc5..c75909c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,10 @@ services: - revproxy environment: - NODE_ENV=production + # No need to publish ports to host, but expose them to container network + expose: + - 3000 + - 3001 # Define networks to connect to external services networks: diff --git a/src/lib/connection/MudConnection.ts b/src/lib/connection/MudConnection.ts index ed9ae93..177d31b 100644 --- a/src/lib/connection/MudConnection.ts +++ b/src/lib/connection/MudConnection.ts @@ -51,14 +51,21 @@ export class MudConnection extends EventEmitter { return; } - // Connect through the standalone WebSocket server on port 3001 + // Determine the WebSocket URL based on environment const wsProtocol = window.location.protocol === 'https:' ? 'wss' : 'ws'; - const wsHost = `${window.location.hostname}:3001`; + let wsUrl; - const url = `${wsProtocol}://${wsHost}/mud-ws?host=${encodeURIComponent(this.host)}&port=${this.port}&useSSL=${this.useSSL}`; - console.log('Connecting to WebSocket server:', url); + // In development, use port 3001 + if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') { + wsUrl = `${wsProtocol}://${window.location.hostname}:3001/mud-ws?host=${encodeURIComponent(this.host)}&port=${this.port}&useSSL=${this.useSSL}`; + } else { + // In production, use the same domain & port as the web app + wsUrl = `${wsProtocol}://${window.location.host}/mud-ws?host=${encodeURIComponent(this.host)}&port=${this.port}&useSSL=${this.useSSL}`; + } - this.webSocket = new WebSocket(url); + console.log('Connecting to WebSocket server:', wsUrl); + + this.webSocket = new WebSocket(wsUrl); this.webSocket.binaryType = 'arraybuffer';