2

I'm creating an online chat on my website.

Almost everything is okay: a client can join a room, he can talk with other clients in the room BUT he can't leave it.

Indeed, when a client try to disconnect a room, it doesn't work.

Here is my server-side code:

io.sockets.on('connection', function (socket) {
    // connection
    socket.on('room', function(room) {
        socket.join(room);
        console.log('room join :'+room);
    });

    // some actions (message, etc)

    // disconnection
    socket.on('leave', function(room) {
        socket.leave(room);
        console.log('room quit :'+room);
    });
});

I get the "room quit : xxx" in my console but when I try to talk in the room, that still works!
My client can't be disconnected from the room. What goes wrong?

fredtantini
  • 15,966
  • 8
  • 49
  • 55
Alan
  • 47
  • 3
  • 12
  • Does `typeof room == "string"` in your `leave` event handler? Also, possibly check `room.length` to make sure you don't have any trailing whitespace that would cause the `join` room and the `leave` room to differ (but would be impossible to see in a `console.log`). – apsillers Oct 01 '14 at 15:41
  • If there are no other errors showing, it would appear that you either don't have the right socket (perhaps there's more than one socket from that client) or you don't have the exact right room name because join and leave fundamentally work. – jfriend00 Oct 01 '14 at 16:21
  • I'll try to check if everything is ok. Can you confirme that "leave" actually works ? And that it "cut" the connection between users (means that if client who leave the room cannot write in and others clients cannot write to the client who leave) ? – Alan Oct 02 '14 at 07:39
  • After team : room.lenght = 1 and the calue is the same on join and leave. When i try socket.disconnect(), that works. It's only socket.leave which doesn't works – Alan Oct 02 '14 at 07:56
  • 1
    How do you talk to the room? Show a bit of client side code. – DerM Oct 02 '14 at 08:38
  • Please see my answer here : https://stackoverflow.com/a/66251445/3904109 – DragonFire Feb 17 '21 at 23:28

0 Answers0