This repository has been archived on 2026-03-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cau-praktikum/app/static/js/ws.js
T

35 lines
896 B
JavaScript

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;
}