I am creating a turn-based multiplayer board game with socket.io. If a client moved on the board, I want to disable to move once again, but I can't figure out how to achieve this.
When a client moves, it sends the board state to the server, then the server sends the board state to every connected client. Even to the one who made the move.
This is how I send the board state to the server from the client side:
sock.emit("message", board, turn); // Sending the board state to the server
canvas.removeEventListener("click", game);
This is how the server gets the updated board state and sends it to every client:
// If we recieve the board state from a client
sock.on("message", (board, turn) => {
io.emit("message", board, turn); // Sending the board state to every client who is connected
});
And this how the client code handles when it gets a new board state from the server:
sock.on("message", (board, turn) => {
this.board = board;
this.turn = turn;
render(board);
canvas.addEventListener("click", game);
setTimeout(function () {
if (winningMove(board)) {
// disable click event on both client
// tell the server that the game ended
sock.emit("end");
sock.on("end", () => {
window.alert("Game Over...");
window.location.reload();
});
}
}, 100);
});