socket.io 没有发送到所有房间
socket.io doesn't send to all room
我正在使用 Socket.io 和 react 做一个聊天室,当新用户加入聊天室时,我想向所有客户发送更新。
const updateDiscussionClients = room => {
let currentClients = RoomClients({room, io, connections})
console.log(currentClients); // printing correct data
console.log(io.sockets.adapter.rooms.get(room));
socket.to(room).emit('clients_discussion', currentClients)
}
socket.on("join_discussion", (data) => {
let room = 'room_' + data.id + data.name
socket.join(room)
updateDiscussionClients(room)
})
一切正常,但老客户收到的消息,没有最后一个
日志输出:
[
{ name: 'Mohamad Zbib', id: '202100006' },
{ name: 'pu10', id: '10' }
]
Set(2) { 'pbKuaEdrfg7n83YyAAAD', '3QYOJCzlxxMOLquzAAAB' }
事件被pbKuaEdrfg7n83YyAAAD
收到,但是3QYOJCzlxxMOLquzAAAB
没有收到,如果以后有更新他会收到
是该事件的 3QYOJCzlxxMOLquzAAAB 发送者,哪个不会接收?
socket.to("room1").emit(/* ... */);
将发送给房间内除发件人以外的所有人。
io.in("room1").emit(/* ... */);
将发送给房间内的所有人
查看此 cheat sheet 了解更多信息
我正在使用 Socket.io 和 react 做一个聊天室,当新用户加入聊天室时,我想向所有客户发送更新。
const updateDiscussionClients = room => {
let currentClients = RoomClients({room, io, connections})
console.log(currentClients); // printing correct data
console.log(io.sockets.adapter.rooms.get(room));
socket.to(room).emit('clients_discussion', currentClients)
}
socket.on("join_discussion", (data) => {
let room = 'room_' + data.id + data.name
socket.join(room)
updateDiscussionClients(room)
})
一切正常,但老客户收到的消息,没有最后一个
日志输出:
[
{ name: 'Mohamad Zbib', id: '202100006' },
{ name: 'pu10', id: '10' }
]
Set(2) { 'pbKuaEdrfg7n83YyAAAD', '3QYOJCzlxxMOLquzAAAB' }
事件被pbKuaEdrfg7n83YyAAAD
收到,但是3QYOJCzlxxMOLquzAAAB
没有收到,如果以后有更新他会收到
是该事件的 3QYOJCzlxxMOLquzAAAB 发送者,哪个不会接收?
socket.to("room1").emit(/* ... */);
将发送给房间内除发件人以外的所有人。
io.in("room1").emit(/* ... */);
将发送给房间内的所有人
查看此 cheat sheet 了解更多信息