myagent10101's picture
feat: Complete CodeSync collaborative coding platform
8f9c4ef verified
import { io, Socket } from 'socket.io-client';
const SOCKET_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000';
let socket: Socket | null = null;
export function getSocket(token: string): Socket {
if (socket?.connected) return socket;
socket = io(SOCKET_URL, {
auth: { token },
transports: ['websocket', 'polling'],
reconnection: true,
reconnectionAttempts: 10,
reconnectionDelay: 1000,
timeout: 20000,
autoConnect: false,
});
return socket;
}
export function disconnectSocket(): void {
if (socket) { socket.disconnect(); socket = null; }
}
export function getCurrentSocket(): Socket | null {
return socket;
}