Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

109
Vistas
Avoid multiple moves on socket.io multiplayer board game

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);
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use the socket.id value to uniquely identify each connected client (player/board in your case) and add logic on the server side to prevent multiple moves from the same client.

Option 1

Keep track of the client that made the last move and ignore any new moves from the same client. The implementation would look like this:

  // Variable to track last played client
  let lastPlayedClient = null;

  // If we recieve the board state from a client
  sock.on("message", (board, turn) => {
    // Ensure move is made by different client
    if (lastPlayedClient !== sock.id) {
      // Update last played client to current client id
      lastPlayedClient = sock.id;
      io.emit("message", board, turn); // Sending the board state to every client who is connected
    }
  });
 

Option 2

On the server side, add logic to identify the client who has to make the next move. Then allow only that client to make the next move. Ignore moves made by any other client. The implementation would look like this:

  // Variable to track client id of next player
  let nextPlayer = yourLogicToIdentifyNextPlayer();

  // If we recieve the board state from a client
  sock.on("message", (board, turn) => {
    // Ensure move is made by the identified client only
    if (nextPlayer === sock.id) {
      io.emit("message", board, turn); // Sending the board state to every client who is connected
    }
  });

To improve user experience, you can disable input on the board on the frontend as soon as the first move is made.

Ref: Socket#id

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood your question correctly, you want to send move signal to all clients except to the client who made request (move) right ?.

then use socket.broadcast.emit for that

  // 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
    socket.broadcast.emit("message", board, turn) // sends to every client except the client who made this request (socket.id).
  });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda