在以下功能上使用io.sockets.adapter.rooms[room]
可以获取特殊房间的所有套接字ID,现在我尝试将套接字ID推送为房间客户端,但是不幸的是,当我尝试在undefined
上使用socketIds[i]
时cc>
例如:
socketIds:{"sockets":{"yJJj6ysKEPp0UhOdAAAA":true,"9dDHXedsMRrjJNhrAAAB":true},"length":2}
如何使用
clients.push(chatClients[socketIds[i]]);
从sockets
中获取所有socketIds
?function getClientsInRoom(socketId, room) {
// get array of socket ids in this room
var socketIds = io.sockets.adapter.rooms[room];
console.log("socketIds:" + JSON.stringify(socketIds));
var clients = [];
if (socketIds && socketIds.length > 0) {
//socketsCount = socketIds.lenght;
// push every client to the result array
for (var i = 0, len = socketIds.length; i < len; i++) {
// check if the socket is not the requesting
// socket
if (socketIds[i] != socketId) {
clients.push(chatClients[socketIds[i]]);
}
}
}
return clients;
}
最佳答案
此代码解决的问题
function getClientsInRoom(socketId, room) {
// get array of socket ids in this room
var socketIds = io.sockets.adapter.rooms[room];
var clients = [];
if (socketIds && socketIds.length > 0) {
//socketsCount = socketIds.lenght;
// push every client to the result array
for (var i = 0, len = socketIds.length; i < len; i++) {
// check if the socket is not the requesting
// socket
if (socketIds.sockets[i] != socketId) {
clients.push(chatClients[Object.keys(socketIds.sockets)[i]]);
}
}
}
return clients;
}