1

Please, read to the end, help me!

I'm developing a multiroom game using SocketIO, with a client-side (React) and a server-side (Node). I'm having a problem on handling this situation:

Consider that at least two clients are inside a Room, after have joining it with socket.join(roomId) (at server-side).

Situation 1: When a player leaves the room manually (clicking a button in the page), the client-side does socket.emit("playerLeaving", someVar) to the server-side listener socket.on("playerLeaving", doStuff) that do some important things with the database (let's call it X listener). Then this listener fires back an event to the other client sockets in that room, with io.sockets.in(roomID).emit("playerLeft", someVar). No problem here.

Situation 2: Instead of manually leaving that room, that user just close the tab/browser, which fires a "disconnect" event do the server-side. Event in that case, the server must do the same procedure that he did on 1st situation, inside that X listener, so I just tried to emit this event socket.emit("playerLeaving", roomID) from inside the "disconnect" handler at server-side, so the X listener would receive it, do his stuffs, and broadcast to the other players that someone has left that room.

THE PROBLEM: the "disconnect" handler (at server-side) emits to X listener (also server-side), but this one doesn't receives it. Summin up, when a player in a room closes the tab/browser, the server isn't handling it correctly and consequently the other players in that room doesn't even get to know about that.

MY CODE:

SERVER-SIDE:

io.sockets.on('connection', (socket) => {
    
    socket.on("playerJoiningRoom", (roomID) => {
      socket.join(roomID); // to sign socket to a room
      // .... some code
      io.broadcast.to(roomID).emit("someoneJoined", someVar); // this works fine
    })

    // X-listener
    socket.on("playerLeaving", (someVar) => {
      // ... do stuff in database
      io.sockets.in(roomID).emit("playerLeft", otherVar);
    }

    socket.on("disconnect", () => {
      // ...some code here
      // then, emit to the listener above
      // example:  socket.emit("playerLeaving", someVar);

    });

  })

}

CLIENT-SIDE

function socketEnteredRoom(socket, roomID) {
    // notify server
    socket.emit("playerJoiningRoom", roomID);

    // listen to other players joining the room
    socket.on("someoneJoined", someVar);

    // listen to other players leaving the room. This listener works when some player leave manually (leave button), but don't work when a player closes tab/browser
    socket.on("someoneLeft", someVar);
}

// when player wants to exit room
function handlerExitRoom() {
   socket.emit("playerLeaving", someVar);
}

Waiting for your help! Thank you! (sorry if this is a repeated question, but I couldn't find a answer that worked)

João Vitor
  • 83
  • 1
  • 5

2 Answers2

1

I'm not sure since I can't see your full code, but a common issue is people try to emit to rooms their socket is in in disconnect but the socket is no longer in any rooms at this point. Try changing to disconnect to disconnecting.

Ryan Soderberg
  • 585
  • 4
  • 21
0

Try this,

//server-side
socket.on("disconnect", () => {
  io.emit("playerLeaving", someVar);
});


//client-side
socket.emit("disconnect", () => {
  
});
Nithin K Joy
  • 943
  • 8
  • 14