Attempt websocket fix

This commit is contained in:
2025-04-21 23:43:50 +02:00
parent 2e265c08f1
commit 2cff41f187
3 changed files with 23 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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:

View File

@@ -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';