我想使用ng-socket-io
加入一个房间。我知道Socket.IO supports rooms and namespaces所以它确实是Socket.IO所能实现的。
我基本上希望连接用户加入一个房间,所以我可以在后端区分它们。这是我现在所做的一个例子,我无法在后端工作:
this.socket.emit("setRoom", room.id);
然后我在后端抓住它:
io.on('connection', (socket) => {
socket.on('setRoom', (roomId) => {
socket.roomId = roomId;
});
});
然后我基本上想要做这样的事情,当我想向那个房间发出新消息时:
io.clients.forEach(client => {
if(client.roomId === roomId) {
client.emit("newMessage", "some message to those only");
}
});
ng-socket-io
是否可以实现这一目标?
答案 0 :(得分:0)
我认为这与ng-socket-io没有任何关系,你只需要在后端。
而不是......
io.clients.forEach(client => {
if(client.roomId === roomId) {
client.emit("newMessage", "some message to those only");
}
});
尝试使用最新版本的socket-io ...
_.forEach(io.sockets.sockets, (socket) => {
if(socket.roomId === roomId) {
io.to(roomId).emit("newMessage", "some message to those only");
}
});