finish ws client & handlers
This commit is contained in:
+7
-8
@@ -1,35 +1,34 @@
|
|||||||
// using socketio for websocket communication
|
|
||||||
export function createWSClient(handlers = {}) {
|
export function createWSClient(handlers = {}) {
|
||||||
const socket = io();
|
const socket = io();
|
||||||
|
|
||||||
socket.on("connect", () => {
|
socket.on("connect", () => {
|
||||||
console.log("Connected to server");
|
handlers.onConnect?.(socket);
|
||||||
handlers.onConnect?.();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("disconnect", () => {
|
socket.on("disconnect", () => {
|
||||||
console.log("Disconnected from server");
|
|
||||||
handlers.onDisconnect?.();
|
handlers.onDisconnect?.();
|
||||||
});
|
});
|
||||||
|
|
||||||
const serverEvents = {
|
const serverEvents = {
|
||||||
|
server_ready: handlers.onServerReady,
|
||||||
code_game_created: handlers.onGameCreated,
|
code_game_created: handlers.onGameCreated,
|
||||||
code_game_joined: handlers.onGameJoined,
|
code_game_joined: handlers.onGameJoined,
|
||||||
|
code_game_join_failed: handlers.onGameJoinFailed,
|
||||||
game_started: handlers.onGameStarted,
|
game_started: handlers.onGameStarted,
|
||||||
p2_connected: handlers.onP2Connected,
|
p2_connected: handlers.onP2Connected,
|
||||||
user_move: handlers.onUserMove,
|
user_move: handlers.onUserMove,
|
||||||
move_accept: handlers.onMoveAccept,
|
move_accept: handlers.onMoveAccept,
|
||||||
move_reject: handlers.onMoveReject,
|
move_reject: handlers.onMoveReject,
|
||||||
game_over: handlers.onGameOver,
|
game_over: handlers.onGameOver,
|
||||||
//todo: draw, resign, rematch
|
draw_offer: handlers.onDrawOffered,
|
||||||
|
clock_tick: handlers.onClockTick,
|
||||||
};
|
};
|
||||||
|
|
||||||
let i = 0;
|
|
||||||
for (const [event, handler] of Object.entries(serverEvents)) {
|
for (const [event, handler] of Object.entries(serverEvents)) {
|
||||||
if (handler) {
|
if (handler) {
|
||||||
socket.on(event, handler);
|
socket.on(event, handler);
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("registered " + i + " server event handlers");
|
|
||||||
|
return socket;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,43 @@
|
|||||||
export function handleGameStarted(data) {
|
export function handleGameStarted(data) {
|
||||||
throw new Error("todo");
|
window.playApp?.onGameStarted(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleCodeGameCreated(data) {
|
export function handleCodeGameCreated(data) {
|
||||||
throw new Error("todo");
|
window.playApp?.onCodeCreated(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function handleP2Connected(data) {
|
export function handleP2Connected(data) {
|
||||||
throw new Error("todo");
|
window.playApp?.onP2Connected(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleCodeGameJoined(data) {
|
||||||
|
window.playApp?.onGameJoined(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleCodeGameJoinFailed(data) {
|
||||||
|
window.playApp?.onGameJoinFailed(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleMoveAccepted(data) {
|
||||||
|
window.playApp?.onMoveApplied(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleOpponentMove(data) {
|
||||||
|
window.playApp?.onMoveApplied(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleMoveRejected(data) {
|
||||||
|
window.playApp?.onMoveRejected(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleGameOver(data) {
|
||||||
|
window.playApp?.onGameOver(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleDrawOffered(data) {
|
||||||
|
window.playApp?.onDrawOffered(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function handleClockTick(data) {
|
||||||
|
window.playApp?.onClockTick(data);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user