Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJs socket.io获取房间中的所有客户端_Javascript_Node.js_Sockets_Socket.io - Fatal编程技术网

Javascript NodeJs socket.io获取房间中的所有客户端

Javascript NodeJs socket.io获取房间中的所有客户端,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,使用下面的函数io.sockets.adapter.rooms[room]我可以获得特殊房间的所有插座id,现在我尝试将插座id作为房间客户端推送,但不幸的是,当我尝试在客户端上使用时,我在socketID[i]上得到了未定义的 例如: socketIds:{"sockets":{"yJJj6ysKEPp0UhOdAAAA":true,"9dDHXedsMRrjJNhrAAAB":true},"length":2} 如何使用length从socketId获取所有socket function

使用下面的函数
io.sockets.adapter.rooms[room]
我可以获得特殊房间的所有插座id,现在我尝试将插座id作为房间客户端推送,但不幸的是,当我尝试在
客户端上使用时,我在
socketID[i]
上得到了
未定义的

例如:

socketIds:{"sockets":{"yJJj6ysKEPp0UhOdAAAA":true,"9dDHXedsMRrjJNhrAAAB":true},"length":2}
如何使用
length
socketId
获取所有
socket

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;
}
函数getClientsInRoom(socketId,room){
//获取此房间中的套接字ID数组
var socketIds=io.sockets.adapter.rooms[room];
log(“socketIds:+JSON.stringify(socketIds));
var客户=[];
if(socketIds&&socketIds.length>0){
//SocketScont=socketIds.Length;
//将每个客户端推送到结果数组
对于(变量i=0,len=socketIds.length;i
此代码已解决问题

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;
}
函数getClientsInRoom(socketId,room){
//获取此房间中的套接字ID数组
var socketIds=io.sockets.adapter.rooms[room];
var客户=[];
if(socketIds&&socketIds.length>0){
//SocketScont=socketIds.Length;
//将每个客户端推送到结果数组
对于(变量i=0,len=socketIds.length;i
此代码已解决问题

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;
}
函数getClientsInRoom(socketId,room){
//获取此房间中的套接字ID数组
var socketIds=io.sockets.adapter.rooms[room];
var客户=[];
if(socketIds&&socketIds.length>0){
//SocketScont=socketIds.Length;
//将每个客户端推送到结果数组
对于(变量i=0,len=socketIds.length;i