Spaces:
Sleeping
Sleeping
File size: 718 Bytes
149698e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import { io, type Socket } from 'socket.io-client';
let socket: Socket | null = null;
export function getSocket(): Socket {
if (!socket) {
socket = io(window.location.origin, {
path: '/ws',
autoConnect: false,
withCredentials: true,
reconnection: true,
reconnectionAttempts: Infinity,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
});
}
return socket;
}
export function connectSocket(): void {
const s = getSocket();
if (!s.connected) {
// Socket will authenticate via cookie (token is sent as cookie in the handshake)
s.connect();
}
}
export function disconnectSocket(): void {
if (socket?.connected) {
socket.disconnect();
}
}
|