我已经在谷歌上搜索了 2 天,以使用 nodeJs 在 socket.io 上动态创建房间。当我为服务器创建一个房间时,我是这样的:
socket.on('follow_me', function (user_id) { //I use the user_id of the user to create room
socket.join(user_id);
console.log(server.sockets.manager.rooms);
});
如果接下来,我想通过使用向所有连接的人发送消息
socket.join(user_id);
当我使用:
socket.on('message', function (data) {
socket.broadcast.to(data.room).emit('recieve',data.message); //emit to 'room' except this socket
});
这个房间的其他用户没有收到消息;但如果我使用:
socket.join(user_id);`,when i use :
socket.on('message', function (data) {
socket.broadcast.emit('recieve',data.message);
});
所有用户都收到消息。为什么房间不适合我?我认为代码很好!
坦克的