Refactor WebSocket connection logic to differentiate between localhost and production environments

This commit is contained in:
Anton Roslund 2026-01-02 22:52:12 +01:00
parent aadd038880
commit f7fbb38961

View file

@ -4340,9 +4340,11 @@
function connectWebSocket() { function connectWebSocket() {
// Determine WebSocket URL - use same hostname as current page, port 8081 // Determine WebSocket URL - use same hostname as current page, port 8081
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsHost = window.location.hostname; // Heuristic: if running on localhost, use port 8081; otherwise use /ws path via Nginx
const wsPort = '8081'; const isLocalhost = location.hostname === 'localhost' || location.hostname === '127.0.0.1';
const wsUrl = `${wsProtocol}//${wsHost}:${wsPort}`; const wsUrl = isLocalhost
? `${wsProtocol}//${location.hostname}:8081`
: `${wsProtocol}//${location.host}/ws`;
console.log('Connecting to WebSocket:', wsUrl); console.log('Connecting to WebSocket:', wsUrl);
ws = new WebSocket(wsUrl); ws = new WebSocket(wsUrl);