From 06912c6885f0a12196d5a3e5019c5a1457e171f8 Mon Sep 17 00:00:00 2001 From: DarkSky <25152247+darkskygit@users.noreply.github.com> Date: Thu, 21 Dec 2023 23:52:05 +0800 Subject: [PATCH] fix: websocket prefix (#5372) --- .../frontend/workspace/src/utils/affine-io.ts | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/frontend/workspace/src/utils/affine-io.ts b/packages/frontend/workspace/src/utils/affine-io.ts index b3d4afb630..71055dd5a4 100644 --- a/packages/frontend/workspace/src/utils/affine-io.ts +++ b/packages/frontend/workspace/src/utils/affine-io.ts @@ -2,21 +2,25 @@ import { Manager } from 'socket.io-client'; let ioManager: Manager | null = null; +function getBaseUrl(): string { + if (environment.isDesktop) { + return runtimeConfig.serverUrlPrefix; + } + const { protocol, hostname, port } = window.location; + return `${protocol === 'https:' ? 'wss' : 'ws'}://${hostname}${ + port ? `:${port}` : '' + }`; +} + // use lazy initialization socket.io io manager export function getIoManager(): Manager { if (ioManager) { return ioManager; } - const { protocol, hostname, port } = window.location; - ioManager = new Manager( - `${protocol === 'https:' ? 'wss' : 'ws'}://${hostname}${ - port ? `:${port}` : '' - }/`, - { - autoConnect: false, - transports: ['websocket'], - secure: location.protocol === 'https:', - } - ); + ioManager = new Manager(`${getBaseUrl()}/`, { + autoConnect: false, + transports: ['websocket'], + secure: location.protocol === 'https:', + }); return ioManager; }