简体   繁体   English

如何在socket.io中连接房间

[英]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" 我没有使用socket.io 文档来与Room系统创建聊天,但是当我在客户端执行临时代码时,它返回“未定义”

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

Because io not has function to() 因为io没有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 . 另一个问题是,代码不应该返回任何值(据我所知),因此如果没有返回值,它将返回undefined

Edit: to switch, you use this syntax: 编辑:切换,您使用以下语法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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