调用 .disconnect() 后如何重新连接 [英] How to reconnect after you called .disconnect()

查看:47
本文介绍了调用 .disconnect() 后如何重新连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在您发出手动 .disconnect() 后,如何将客户端重新连接到服务器?

Question: How do you reconnect a client to the server after you have issued a manual .disconnect()?

在我当前的项目中,当用户从会话中注销时,我需要断开客户端与服务器的连接.我做了一个 socket.disconnect() 来成功断开连接.服务器从会话中删除了用户.

In my current project, I need to disconnect a client from the server when the user log out from the session. I did a socket.disconnect() to do the disconnection successfully. Server removed the user from the session.

一段时间后,用户决定再次登录,但socket.io拒绝连接.

After awhile, the user decided to login again, but socket.io refuses to connect.

我很清楚 Socket.IO 已经实现了重新连接算法,但显然这是一个不同的情况.

I am clear that Socket.IO has implemented reconnection algorithm but clearly this is a different case.

下面是我进行连接的一段代码.在此代码块的第二次触发时,创建了对象 socket,但没有从该对象触发 connect.

Below is the piece of code where I do the connection. On second trigger of this block of code, object socket was created, but no connect was fired from this object.

//Start the socket
var socket = io.connect(SOCKET_IO_URL);
socket.on('connect', function() {
    me.fireEvent('connect');
    socket.emit('register', {
        hashed_identifier: hashed_identifier,
        account_id: account_id
    });
});

socket.on('ready', function() {
    me.ready = true;
    me.log('Handshake done. Listening for new data');
});

socket.on('data', function(data) {
    me.fireEvent('data', data);
    //Tells server we received it
    socket.emit('data', {ack: 1});
});

socket.on('disconnect', function() {
    me.fireEvent('disconnect');
});

更新:根据@Tony 的要求

其实整个东西都包裹在Sencha Touch 2.0之下,但我相信与ST2.0无关

In fact the whole thing is wrapped under Sencha Touch 2.0, but I believe there is nothing to do with ST2.0

这是我的数据类.这个类的用法是当用户登录时,这个类会被初始化.并且在用户注销时,系统会调用该类中的disconnect()方法.

This is my Data Class. Usage of this class is when the user logged in, this class will get initialized. And upon the user logout, the system will call the disconnect() method in this class.

当用户再次登录时,这个类再次被初始化,但有趣的是套接字以某种方式保留了它之前所有的事件和会话.

When the user login again, this class is initialized again, but funny is the socket somehow retained all the previous events and sessions it has previously.

/**
* Serve as interface to wait for data communication in between server and client
*/
Ext.define('go.module.connect.Data', {

    mixins: {
        observable: 'Ext.mixin.Observable'
    },

    config: {
        account: null
    },

    ready: false,

    socket: null,

    constructor: function(cfg) {
        var me = this,
            hashed_identifier = Sha1.hash(go.__identifier);


        me.initConfig(cfg);

        var account_id = me.getAccount().get('id');

        //Start the socket
        var socket = io.connect(SOCKET_IO_URL);
        socket.on('connect', function() {
            console.log('connect');
            me.fireEvent('connect');
            socket.emit('register', {
                hashed_identifier:hashed_identifier,
                account_id: account_id
            });
        });

        socket.on('ready', function() {
            me.ready = true;
            me.log('Handshake done. Listening for new data');
        });

        socket.on('data', function(data) {
            me.fireEvent('data', data);
            //Tells server we received it
            socket.emit('data', {ack: 1});
        });

        socket.on('disconnect', function() {
            me.fireEvent('disconnect');
        });

        console.log(socket);
        if (!socket.socket.connected){
            socket.socket.reconnect();
        }


        me.socket = socket;


        me.callParent([cfg]);
    },

    disconnect: function() {
        this.socket.disconnect();
        this.socket.removeAllListeners();
        this.socket.socket.removeAllListeners();
    },

    log: function(msg) {
        console.log('@@ Connect: '+msg);
    }
});

下面是我的 console.log 结果:

And below is my console.log results:

下面是我的 node.js 调试窗口

And below is my node.js debug window

我相信这个有趣场景的根本原因是之前附加的 connect 事件侦听器没有彻底删除.我应该如何删除它?我应该使用一次吗?或者我也应该指定监听器函数.我认为 removeAllListeners() 足以完成这项任务.

I believe the root cause of this funny scenario is that the previously attached connect event listener is not removed thoroughly. How should I remove it? Should I use once? or I should specify the listener function as well. I thought removeAllListeners() is sufficient for this task.

推荐答案

最新的 socket.io 标准做法是:

The standard approach in latest socket.io is:

socket.on('disconnect', function() {
    socket.socket.reconnect();
}

这是我一直在我的应用程序中使用的,并且效果很好.它还确保套接字在服务器正常运行时不断尝试重新连接,并最终在服务器重新联机时重新连接.

This is what I've been using in my app and works great. It also ensures that the socket keeps trying to reconnect if the server goes way, and eventually reconnects when the server is back online.

就您而言,您需要确保两件事:

In your case, you need to ensure two things:

  1. 您只需创建一次套接字.不要多次调用 socket = io.connect(...).
  2. 您只需设置一次事件处理 - 否则它们将被多次触发!

所以当你想重新连接客户端时,调用socket.socket.reconnect().您还可以从 FireFox 和 Chrome 中的浏览器控制台对此进行测试.

So when you want to reconnect the client, call socket.socket.reconnect(). You can also test this from the browser console in FireFox and Chrome.

这篇关于调用 .disconnect() 后如何重新连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆