繁体   English   中英

无法使用本地主机上的node.js向不在线的人发送消息

[英]Unable to send messages to who are not online using node.js on my localhost

我已经启动了节点并使用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]);
        }   
    }
});

数据是一个JSON对象。 如果我有任何data [i] .message_to离线,我只能将其发送给客户端,而不能发送给其他套接字。 如果我在线上有所有条目,则可以将其发送到客户端和其他套接字

我想出了另一种方法

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);
            }   
        });
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM