Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SocketIO - Connect failed after Disconnected #14077

Closed
ZeroHackeR opened this issue Oct 6, 2015 · 21 comments
Closed

SocketIO - Connect failed after Disconnected #14077

ZeroHackeR opened this issue Oct 6, 2015 · 21 comments
Labels
Milestone

Comments

@ZeroHackeR
Copy link
Contributor

Setup:

  • Platform: Android
  • Framework: 3.8 (JS) (with SocketIO patch for incorrect packet format)
  • Create a button to Connect to any working SocektIO (v1+) server
  • Create on SocketIO's connect and disconnect event handlers

Test Flow:

  • Connect to server
  • You should receive connect event
  • Disable your Wifi/Mobile Data
  • You should receive disconnect event
  • Now, enable your Wifi/Mobile Data
  • Try to connect again
  • No event fire
@man2
Copy link

man2 commented Oct 6, 2015

I think it's because cocos2d-x socket.io implementation haven't implemented reconnect even. I don't know how to do it also. I tried adding on "reconnect" event with no success

@ZeroHackeR
Copy link
Contributor Author

Fixed:

image

Update: ZeroHackeR@4cfe139

@ZeroHackeR
Copy link
Contributor Author

@man2 Yes.. there is no reconnect methods. We got to manually monitor and connect again if it gets disconnected. And there was a bug as above when tried to connect again.

@man2
Copy link

man2 commented Oct 6, 2015

thanks for the fix.. awesome..

@ZeroHackeR
Copy link
Contributor Author

@pandamicro Please check and merge

@pandamicro
Copy link
Contributor

Thanks for the fix, it have been reviewed and merged

@pandamicro pandamicro added this to the v3.9 milestone Oct 8, 2015
@ZeroHackeR
Copy link
Contributor Author

Glad to be a part of this awesome engine ;)

@pandamicro
Copy link
Contributor

The pleasure is ours ~

@Super-Man
Copy link

When the server disconnects 。 the client response disconnect function. Re-connect Crash。
How to reconnect socket ? please give an example 。

sioManager.init = function(){
sioManager._clientSio = SocketIO.connect("www.xxx.ur",{"force new connection": true });
sioManager._clientSio.on("connect",function(){
});
sioManager._clientSio.on("disconnect",sioManager.disconnect);
};

sioManager.disconnect = function(){
sioManager._clientSio.disconnect();
sioManager._clientSio = null;
sioManager.init();
}

this is my code , i don't find where problem>...

@ZeroHackeR
Copy link
Contributor Author

@Super-Man what is your cocos version? make sure your socketio.cpp has the fixes as above...

and try to add some delay before calling init...

@Super-Man
Copy link

@ZeroHackeR I try to disconnect the response received when a record label, indicating disconnected. Then call disconnect (); time to get in the timer status is disconnected, go to init () but the connection again, unsuccessful.
And call disconnect (),the project will be crash.

this is my code

var sioManager = sioManager || {};
sioManager._sioClient = null;
sioManager.connectState = 1; // 0: connect success ,1: connecting 2: disconnect
sioManager.sendReconnectTime = 0 ; // reconnect time

// sio init
sioManager.init = function(){
sioManager.connectState = 1;
sioManager._sioClient = SocketIO.connect(" 192.168.10.11",{"force new connection" : true});
sioManager._sioClient.on("connect",function(){
sioManager.connectState = 0;
});

sioManager._sioClient.on("disconnect",sioManager.disconnection);
sioManager._sioClient.on("error",sioManager.onerror);
};

sioManager.disconnection = function(){
if(sioManager.connectState == 0){
sioManager.connectState = 2;
sioManager._sioClient.disconnect(); // there response application will crash . on SocketImple.onColse
sioManager._sioClient = null;
} ;
};

In the main scene. schedule function,

this.schedule(this.reconnectSio,1);

reconnectSio :function(dt){
if(sioManager.connectState == 2 && (G_severTime - sioManager.sendReconnectTime) >= 5000){
sioManager.init();
}
},

@ZeroHackeR
Copy link
Contributor Author

@Super-Man I think, you don't need to call sioManager._sioClient.disconnect() as socket must be already disconnected to reach to "disconnect" event handler.

All the best and I'm already using Socket.IO in two of my games and they are working fine.

Good Luck Bro!

@Super-Man
Copy link

@ZeroHackeR
thank you!
I'll try, but found again in the timer init (), the connection is unsuccessful. SocketIO and no reconnection mechanism ..
I want to reconnect it, how? Can you give me some advice?

@ZeroHackeR
Copy link
Contributor Author

Here're my codes (not full version) using Cocos 3.8 (with custom socketio.cpp fix as above)


    ctor:function () {
        this._super();
        var self = this;
        setInterval(self.isConnected, 10 * 1000);
        return true;
    }, 

    isConnected: function(){
        if(_DataLayer._socket == null || !_DataLayer._socket.connected){
            _DataLayer.setup();
            //cc.game.restart(); // you may restart the game if your server have handler/logic for reconnected users
        }
    },

    setup: function(){
        var self = this;
        cc.log("opening socket connection");
        var sio = SocketIO.connect(SOCKET_URL, {"force new connection" : true, reconnection: false});
        sio.on("connect", self.sioConnect);     
        sio.on("disconnect", self.sioDisconnect);       
        sio.on("error", self.sioError);     
        sio.on("hello", self.sioHello);

        self._socket = sio;
},

@Super-Man
Copy link

@ZeroHackeR OK , i'll check my codes。 thank you !

@Super-Man
Copy link

@ZeroHackeR Very helpless!
I try to reconnect delay also checked cocos version, and socketIO.cpp, .h file. With the 3.8 version of the same. But recall init (), or cause the application to crash, and the connection is unsuccessful.

@linxuanwei
Copy link

function linxuanwei_sio(_uri){
this._socket = null;
this._uri = _uri;
this._isConnected = false;
}

linxuanwei_sio.prototype.connect = function(){
var self = this;
var theSock = SocketIO.connect(self._uri,{"force new connection":true,"reconnection": false});
theSock.on('connect',function(){
console.log('>>>> connect Server success');
self._isConnected = true;
if(self._timer) clearInterval(self._timer);
});
theSock.on('disconnect',function(){
console.log('>>>> disconnect !');
if(self._timer) clearInterval(self._timer);
self._isConnected = false;
self.reconnect();
});
theSock.on('error',function(data){
console.log('>>>> error:'+JSON.stringify(data));
if(self._timer) clearInterval(self._timer);
self._isConnected = false;
self.reconnect();
});
self._socket = theSock;
};

linxuanwei_sio.prototype.reconnect= function(){
var self = this;
// self._socket = null;
self._timer = setInterval(function(){
if(!self._isConnected){
console.log('>>>>> reconnecting....');
self.connect();
}

After server restarted, cocos crashed too.

@linxuanwei
Copy link

function linxuanwei_sio(_uri){
this._socket = null;
this._uri = _uri;
this._isConnected = false;
}

linxuanwei_sio.prototype.connect = function(){
var self = this;
var theSock = SocketIO.connect(self._uri,{"force new connection":true,"reconnection": false});
theSock.on('connect',function(){
console.log('>>>> connect Server success');
self._isConnected = true;
if(self._timer) clearInterval(self._timer);
});
theSock.on('disconnect',function(){
console.log('>>>> disconnect !');
if(self._timer) clearInterval(self._timer);
self._isConnected = false;
self.reconnect();
});
theSock.on('error',function(data){
console.log('>>>> error:'+JSON.stringify(data));
if(self._timer) clearInterval(self._timer);
self._isConnected = false;
self.reconnect();
});
self._socket = theSock;
};

linxuanwei_sio.prototype.reconnect= function(){
var self = this;
// self._socket = null;
self._timer = setInterval(function(){
if(!self._isConnected){
console.log('>>>>> reconnecting....');
self.connect();
}

@ZeroHackeR server restarted , crashed! I wrong?

@linxuanwei
Copy link

image

@ZeroHackeR
Copy link
Contributor Author

@linxuanwei @Super-Man

Thanks for reporting guys...
I will check and try to fix them when I got time...

For time being, please try to restart the game if you got disconnected.
cc.game.restart();

@linxuanwei
Copy link

@ZeroHackeR @Super-Man @pandamicro

I found the reason of crash, after the server rebooted.

when the server stopped, cocos would call method "SIOClientImpl::onClose()", and
method "SIOClientImpl::onClose" would call method "SIOClient::socketClosed()",

then, cocos would call method "connect", but method "connect" called method "SIOClient::disconnect()".

so bug came , method "SIOClient::socketClosed()" and method "SIOClient::disconnect()" both called method "this->release()"

free twice memory.

I added a method

void SIOClient::disconnectNotRelease(){

_connected = false;

_socket->disconnectFromEndpoint(_path);

}

edited method "connect"

    //check if already connected to endpoint, handle
    c = socket->getClient(path);

    if(c == nullptr)
    {
        c = new (std::nothrow) SIOClient(host, port, path, socket, delegate);

        socket->addClient(path, c);

        socket->connectToEndpoint(path);
    }else{
    CCLOG("SocketIO: disconnect previous client");

_ //c->disconnect();
c->disconnectNotRelease();_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants