export function createWSClient(handlers = {}) { const socket = io(); socket.on("connect", () => { handlers.onConnect?.(socket); }); socket.on("disconnect", () => { handlers.onDisconnect?.(); }); const serverEvents = { server_ready: handlers.onServerReady, code_game_created: handlers.onGameCreated, code_game_joined: handlers.onGameJoined, code_game_join_failed: handlers.onGameJoinFailed, game_started: handlers.onGameStarted, p2_connected: handlers.onP2Connected, user_move: handlers.onUserMove, move_accept: handlers.onMoveAccept, move_reject: handlers.onMoveReject, game_over: handlers.onGameOver, draw_offer: handlers.onDrawOffered, clock_tick: handlers.onClockTick, }; for (const [event, handler] of Object.entries(serverEvents)) { if (handler) { socket.on(event, handler); } } return socket; }