Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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 无法使用我的本地主机上的node.js向未联机的用户发送消息_Javascript_Node.js_Callback_Socket.io - Fatal编程技术网

Javascript 无法使用我的本地主机上的node.js向未联机的用户发送消息

Javascript 无法使用我的本地主机上的node.js向未联机的用户发送消息,javascript,node.js,callback,socket.io,Javascript,Node.js,Callback,Socket.io,我已经用socket.io启动并运行了节点。我能够向在我的服务器上联机的人发送消息(他们的套接字连接已打开)。但当我尝试将它发送给多人,至少一人离线时,它无法通过。如果所有人都在线,它就会通过 有关于如何解决这个问题的帮助吗 client.on('message_from_client',function(data){ for(var i=0;i<data.length;i++){ if(data[i].message_to!=''){ cl

我已经用socket.io启动并运行了节点。我能够向在我的服务器上联机的人发送消息(他们的套接字连接已打开)。但当我尝试将它发送给多人,至少一人离线时,它无法通过。如果所有人都在线,它就会通过

有关于如何解决这个问题的帮助吗

client.on('message_from_client',function(data){
    for(var i=0;i<data.length;i++){
        if(data[i].message_to!=''){
            client.emit("update_message_from_server", data[i]);
            if(data[i].message_to != client.username){
                var message_to = data[i].message_to;
                var array =data[i];
                redisClient.SISMEMBER('online',message_to,function(err,reply){
                    if(reply!=0){
                        redisClient.get(message_to,function(err,reply2){
                            if(reply2!=null){
                                io.sockets.sockets[reply2].emit("update_message_from_server",array );
                            }
                        });
                    }else{
                        console.log("Offline");
                    }
                });
            }
        }else{
                client.emit("update_message_from_server", data[i]);
        }   
    }
});
client.on('message\u from\u client',函数(数据){

对于(var i=0;i我想出了另一种方法

client.on('message_from_client',function(data){
        alldata = data.data;
        console.log(alldata);
        alldata = JSON.parse(alldata);
        alldata.forEach(function(data){
            if(data.message_to!=''){
                client.emit("update_message_from_server", data);
                if(data.message_to != client.username){
                    redisClient.SISMEMBER('online',data.message_to,function(err,reply){
                        if(reply!=0){
                            redisClient.get(data.message_to,function(err,reply2){
                                if(reply2!=null){
                                    io.sockets.sockets[reply2].emit("update_message_from_server",data );
                                }
                            });
                        }else{
                            console.log("Offline");
                        }
                    });
                }
            }else{
                    client.emit("update_message_from_server", data);
            }   
        });
    });

你能解释一下答案吗?