fix socket in editor

This commit is contained in:
2026-02-12 19:29:17 +01:00
parent da131834ce
commit e05edff84d
+21 -3
View File
@@ -253,12 +253,30 @@ export const Editor: React.FC = () => {
useEffect(() => { useEffect(() => {
if (!id || !isReady) return; if (!id || !isReady) return;
const socketUrl = import.meta.env.VITE_API_URL === '/api' const apiUrl = import.meta.env.VITE_API_URL || '/api';
const usingRelativeApiBasePath = apiUrl.startsWith('/');
const socketUrl = usingRelativeApiBasePath
? window.location.origin ? window.location.origin
: (import.meta.env.VITE_API_URL || 'http://localhost:8000'); : apiUrl;
const socketPath = (() => {
if (usingRelativeApiBasePath) {
const normalized = apiUrl.endsWith('/') && apiUrl.length > 1 ? apiUrl.slice(0, -1) : apiUrl;
return normalized === '/' ? '/socket.io' : `${normalized}/socket.io`;
}
try {
const parsed = new URL(apiUrl);
const normalizedPath =
parsed.pathname.length > 1 && parsed.pathname.endsWith('/')
? parsed.pathname.slice(0, -1)
: parsed.pathname;
return normalizedPath === '/' ? '/socket.io' : `${normalizedPath}/socket.io`;
} catch {
return '/socket.io';
}
})();
const socket = io(socketUrl, { const socket = io(socketUrl, {
path: '/socket.io', path: socketPath,
transports: ['websocket', 'polling'], transports: ['websocket', 'polling'],
withCredentials: true, withCredentials: true,
}); });