Oprava URL pro websockety

This commit is contained in:
Martin Berka 2023-07-23 00:04:53 +02:00
parent 1c1a8b7111
commit 839f51d8a3

View File

@ -1,11 +1,17 @@
import React from 'react'; import React from 'react';
import socketio from "socket.io-client"; import socketio from "socket.io-client";
import { getBaseUrl } from "../Utils";
// Hack pro socket.io, který si s tečkou neumí poradit let socketUrl;
const socketUrl = getBaseUrl() === '.' ? '/' : getBaseUrl(); let socketPath;
// Záměrně omezeno jen na websocket, aby se případně odhalilo chybné nastavení proxy serveru if (process.env.NODE_ENV === 'development') {
export const socket = socketio.connect(socketUrl, { transports: ["websocket"] }); socketUrl = `http://localhost:3001`;
socketPath = undefined;
} else {
socketUrl = `${window.location.host}`;
socketPath = `${window.location.pathname}socket.io`;
}
export const socket = socketio.connect(socketUrl, { path: socketPath, transports: ["websocket"] });
export const SocketContext = React.createContext(); export const SocketContext = React.createContext();
// Konstanty websocket eventů, musí odpovídat těm na serveru! // Konstanty websocket eventů, musí odpovídat těm na serveru!