2024-05-10 10:26:07 +00:00
|
|
|
import io, {Socket as rawSocket} from "socket.io-client";
|
2024-02-15 22:01:22 +00:00
|
|
|
import type {ServerToClientEvents, ClientToServerEvents} from "../../shared/types/socket-events";
|
2022-06-19 00:25:21 +00:00
|
|
|
|
2024-05-10 10:26:07 +00:00
|
|
|
type Socket = rawSocket<ServerToClientEvents, ClientToServerEvents>;
|
|
|
|
|
|
|
|
const socket: Socket = io({
|
2022-06-19 00:25:21 +00:00
|
|
|
transports: JSON.parse(document.body.dataset.transports || "['polling', 'websocket']"),
|
|
|
|
path: window.location.pathname + "socket.io/",
|
|
|
|
autoConnect: false,
|
|
|
|
reconnection: !document.body.classList.contains("public"),
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ease debugging socket during development
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
window.socket = socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
socket: Socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default socket;
|