thelounge/client/js/socket.ts

25 lines
680 B
TypeScript
Raw Permalink Normal View History

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";
2024-05-10 10:26:07 +00:00
type Socket = rawSocket<ServerToClientEvents, ClientToServerEvents>;
const socket: Socket = io({
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;