简体   繁体   English

Socket.io检测是重新连接还是完全断开

[英]Socket.io Detect whether reconnecting or completely disconnecting

I'm working on a chat application with Node.js and Socket.io, and on disconnect, the remaining user receives an alert saying that their partner disconnected. 我正在使用Node.js和Socket.io开发聊天应用程序,并且在断开连接时,其余用户会收到一条警报,提示其伙伴已断开连接。

The problem is that every once in a while, Socket.io automatically disconnects then reconnects. 问题在于,Socket.io每隔一段时间会自动断开连接,然后重新连接。 My chat application is triggering the alert, even though the partner hasn't really disconnected. 即使伙伴并没有真正断开连接,我的聊天应用程序仍会触发警报。

Code: 码:

var clients = {};
var soloClients = [];
io.sockets.on('connection', function (socket) {

    //Session start
    socket.on('sessionStart', function () {
        clients[socket.id] = socket;
        soloClients.push(socket.id);

        var searchClients = function(){
            if(soloClients.length > 1){
                var rand = Math.floor(Math.random() * soloClients.length);
                if(soloClients[rand] && soloClients[rand] != socket.id){
                    if(clients[soloClients[rand]]){
                        var you = clients[socket.id];
                        var partner = clients[soloClients[rand]]

                        clients[partner.id]['partner'] = you.id;
                        clients[you.id]['partner'] = partner.id;
                        soloClients.splice(soloClients.indexOf(you.id), 1);
                        soloClients.splice(soloClients.indexOf(partner.id), 1);

                        partner.emit('partnerConnect', null);
                        socket.emit('partnerConnect', null);
                    }
                    else{
                        soloClients.splice(rand, 1);
                        searchClients;
                    }
                }
                else{
                    searchClients();
                }
            }
        };
        searchClients();
    });

    //On disconnect
    socket.on('disconnect', function(){
        soloClients.splice(soloClients.indexOf(socket.id), 1);

        if(clients[socket.id]){
            if(clients[clients[socket.id]['partner']]){
                clients[clients[socket.id]['partner']].emit('partnerDisconnect', null);
            }
            delete clients[socket.id];
        }
    });
});

I was wondering if there is any way to solve this. 我想知道是否有办法解决这个问题。

Thanks! 谢谢!

Maybe you should try to find out the real reason why the client gets disconnected? 也许您应该尝试找出导致客户端断开连接的真正原因? Ie is it a bad connection, or firewall issues, or something else? 即是连接不良,防火墙问题还是其他原因?

Also, you should check the error you get from the disconnect, if it's a clean disconnect (no error) you do the normal notification, but if it's an error you maybe want to handle it differently. 另外,您应该检查从断开连接获得的错误,如果是完全断开连接(无错误),则执行常规通知,但是,如果是错误,则可能需要不同的处理方式。

It turns out that this behavior isn't supposed to happen, it was just a bug in that version. 事实证明,这种行为不应该发生,只是该版本中的一个错误。 This bug has been fixed in the latest version. 此错误已在最新版本中修复。

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

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