From 76d723cc86e4c67fb200246dcecd5f3d379cecb6 Mon Sep 17 00:00:00 2001 From: demassimo <87481244+demassimo@users.noreply.github.com> Date: Wed, 1 Oct 2025 08:52:57 +0200 Subject: [PATCH] Fix socket path resolution for custom API bases --- frontend/assets/app.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/frontend/assets/app.js b/frontend/assets/app.js index ab84d11..0330c56 100644 --- a/frontend/assets/app.js +++ b/frontend/assets/app.js @@ -1680,23 +1680,43 @@ setWorkspaceView(workspaceViewDefault); } - function resolveSocketBase() { - const base = state.API || ''; + function resolveSocketEndpoint() { + const DEFAULT_PATH = '/socket.io'; + const base = String(state.API || '').trim(); + + const buildPath = (inputPath) => { + const raw = String(inputPath || ''); + const trimmed = raw.replace(/\/+$/, ''); + const withLeading = trimmed.startsWith('/') ? trimmed : '/' + trimmed; + if (!withLeading || withLeading === '/') return DEFAULT_PATH; + if (withLeading.toLowerCase().endsWith('/api')) { + const prefix = withLeading.slice(0, -4); + return (prefix || '') + DEFAULT_PATH; + } + return withLeading + DEFAULT_PATH; + }; + + if (!base) { + return { origin: '', path: DEFAULT_PATH }; + } + if (/^https?:\/\//i.test(base)) { try { const url = new URL(base); - return url.origin; + return { origin: url.origin, path: buildPath(url.pathname || '/') }; } catch { - return ''; + return { origin: '', path: DEFAULT_PATH }; } } - return ''; + + return { origin: '', path: buildPath(base) }; } function ensureSocket() { if (socket || !state.API) return socket; - const socketBase = resolveSocketBase(); - socket = io(socketBase || undefined, { + const { origin: socketOrigin, path: socketPath } = resolveSocketEndpoint(); + socket = io(socketOrigin || undefined, { + path: socketPath, transports: ['websocket'], auth: { token: state.TOKEN || undefined } });