简体   繁体   中英

How to connect room in socket.io

I'm fallowing socket.io documentation for create a chat with Room system, but when I execute fallowing code in client side, it returning "undefined"

io.to('some room').emit('some event'); // this will returning "undefined"

Because io not has function to()

Where is problem?

It sounds like you never joined the room. The documentation gives this code right before the code you have in your question:

io.on('connection', function(socket) {
  socket.join('some room');
});

Another this is that code shouldn't return any value (as far as I'm aware of), so with no return value it would return undefined .

Edit: to switch, you use this syntax:

socket.leave('some room');
socket.join('another room');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM