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

Connect -> Disconnect -> Connect doesn't work #251

Closed
JonathanJonathanJonathan opened this issue Jul 23, 2011 · 33 comments
Closed

Connect -> Disconnect -> Connect doesn't work #251

JonathanJonathanJonathan opened this issue Jul 23, 2011 · 33 comments

Comments

@JonathanJonathanJonathan

The following code doesn't work for me:
socket = io.connect('http://host.com:8081');socket.disconnect();socket = io.connect('http://host.com:8081');

This is a workaround I've found:
socket = io.connect('http://host.com:8081');socket.disconnect();delete io.sockets['http://host.com:8081'];io.j = [];socket = io.connect('http://host.com:8081');

@aarong
Copy link

@aarong aarong commented Jul 27, 2011

Same issue here -- was unable to connect-disconnect-reconnect the way you described.

However, I've found that the following works. Oddly, if you omit the setTimeout and reconnect immediately (or after too short a timeout) then you are immediately disconnected.

var ns = io.connect(
        '/',
        { reconnect: false }
    ),
    numDisconnects = 2;

ns.on('connect', function () {

    console.log('Connected');

    if (numDisconnects > 0) {
        console.log('Disconnecting');
        ns.socket.disconnect();

        setTimeout(function () {
            console.log('Reconnecting');
            ns.socket.connect();
        }, 5000);
    }
    numDisconnects--;


});

ns.on('disconnect', function () {
    console.log('Disconnected');
});
@Gorokhov
Copy link

@Gorokhov Gorokhov commented Aug 18, 2011

socket = io.connect(host);
socket.disconnect();
socket.socket.reconnect(); || socket.socket.connect(host);
works, but since reconnect or second connect disconnect is fired every couple of seconds

@milani
Copy link

@milani milani commented Aug 29, 2011

I had the same issue so I could not finish my node knockout entry in time due to this bug :(
Please fix it before any other heart break!

@skrat
Copy link

@skrat skrat commented Sep 21, 2011

None of the above workarounds worked for me. Guys, this is serious show stopper. Why the "Unable to reproduce" tag? My testing env. is latest Chrome on OS X 10.6.

@respectTheCode
Copy link

@respectTheCode respectTheCode commented Oct 3, 2011

I am seeing the same thing. Also calling io.connect() a second time fails without an error. Has anyone found a workaround?

@milani
Copy link

@milani milani commented Oct 4, 2011

Use this one: io.connect(null,{'force new connection':true});

@skrat
Copy link

@skrat skrat commented Oct 4, 2011

How convenient, and ugly. What about removing io.sockets[uuri] = socket; from io.js:196? What's the point of that punch line?

@mrjoes
Copy link

@mrjoes mrjoes commented Nov 1, 2011

Unfortunately, same problem here. You can not reconnect after disconnection.

Only thing that worked for me after disconnection:
io.j = [];
io.sockets = [];

Edit: it is not only one problem of the client-side disconnect(). It does not clean up data structures on disconnect - you can see that SocketNamespace.$events still has all callbacks for closed connections, etc.

@milani
Copy link

@milani milani commented Nov 1, 2011

@mrjoes Use io.connect(null,{'force new connection':true});

@skrat
Copy link

@skrat skrat commented Nov 1, 2011

All right, is this really the final API for reconnecting? Rather strange.

@milani
Copy link

@milani milani commented Nov 1, 2011

@skrat I agree with you. there should be an io.reconnect API.

@skrat
Copy link

@skrat skrat commented Nov 1, 2011

I don't think so, io.connect then io.disconnect then io.connect should just work, as expected.

@mrjoes
Copy link

@mrjoes mrjoes commented Nov 1, 2011

@milani: and leak memory due to orphaned sockets, event handlers, etc staying in io.sockets[]?

Not very good idea to be honest.

@milani
Copy link

@milani milani commented Nov 1, 2011

@mrjoes Are you sure about memory leak? @3rd-Eden from io.socket told me to use this API.

@mrjoes
Copy link

@mrjoes mrjoes commented Nov 1, 2011

Nevermind, it won't add socket to io.sockets[] if you pass 'force new connection'.

Though, it is a bit ugly, especially with endpoints, because you're losing multiplexing - it relies on socket in io.sockets to do multiplexing.

@amnjdm
Copy link
Contributor

@amnjdm amnjdm commented Nov 3, 2011

@Gorokhov suggestion of using reconnect through the socket prototype worked well for me:
socket.socket.reconnect()
Thanks

@cacois
Copy link

@cacois cacois commented Nov 27, 2011

Same issue here - socket.reconnect() isn't working for me, I'll try a few other workarounds. This is disappointing though, its a pretty basic piece of functionality to break in an update. I"m sure a lot of applications are unable to easily update because of this.

If devs need any assistance reproducing the issue, I'm happy to help - i'm surprised to see the "unable to reproduce" tag on this bug.

@amnjdm
Copy link
Contributor

@amnjdm amnjdm commented Nov 27, 2011

It appears like there will be a major update soon because of this pull request: #343
@cacois Try using socket.socket.reconnect() rather than socket.reconnect()

@mrjoes
Copy link

@mrjoes mrjoes commented Nov 27, 2011

ah, great news :-)

@cacois
Copy link

@cacois cacois commented Nov 28, 2011

@wackfordjf3 Hey thanks, good call. Works now. :)

@baradas
Copy link

@baradas baradas commented Feb 8, 2012

Still facing issues with reconnect.
2nd reconnect causes multiple reconnection events to be fired on the server. (and this multiplies with every subsequent reconnect). Am firing reconnects on a namespace.

socket = io.connect(url{'reconnect':false});socket.disconnect;// 1st disconnect
socket.socket.reconnect();socket.disconnect(); //2nd disconnect
socket.socket.reconnect(); // 2nd reconnect

Am on : socket.io@0.8.7

Further to this :
Ugly way works fine :
delete io.sockets[url];io.j = [];
socket = io.connect(url);

@zanhsieh
Copy link

@zanhsieh zanhsieh commented Mar 19, 2012

Face the same thing as people already have here; even weird in namespace scenario. Drop the solution for namespacing:

var base = 'http://localhost:3000'
, channel = '/chat'
, url = base + channel;

var socket = io.connect(url);

// call socket.disconnect() won't disconnect, so:
io.sockets[base].disconnect();

// reconnect syntax.
delete io.sockets[base]; io.j =[];
socket = io.connect(url);

note: socket.io version - 0.9.0, platform: windows 7 home premium

@rawberg
Copy link

@rawberg rawberg commented Oct 15, 2012

spent an afternoon wrestling with unit tests that were timing out because I couldn't connect after disconnecting. {'force new connection':true} resolved the issue. thanks @milani

@PEM--
Copy link

@PEM-- PEM-- commented Dec 27, 2012

Same issue here using Cucumber.js. As I have to create and destroy objects, I was having facing the same connect, disconnect, connect failed. Thx @milani and @rawberg

@lin04com
Copy link

@lin04com lin04com commented Feb 19, 2013

It works for me! Thanks @mrjoes.

@Plasma
Copy link

@Plasma Plasma commented Jun 25, 2013

Thanks @milani

@danjunger
Copy link

@danjunger danjunger commented Jun 6, 2014

The 'force new connection' setting is now called 'forceNew' in the v1.0 release for those that might have run into this issue again.

@rauchg
Copy link
Contributor

@rauchg rauchg commented Jun 6, 2014

I pushed a commit to add BC with that, so soon no one should have this issue anymore!

@rauchg rauchg closed this Jun 6, 2014
@pitambar
Copy link

@pitambar pitambar commented Jul 17, 2014

it worked 4 me, socket = io.connect(socketUrl,{'forceNew': true });

@peteruithoven
Copy link

@peteruithoven peteruithoven commented Aug 14, 2014

forceNew disables the whole namespace multiplexing system, is that really the solution?

@maxcodefranco
Copy link

@maxcodefranco maxcodefranco commented Aug 19, 2014

I'm using:
socket= io.connect()
...
// THIS
socket.io.reconnect()
..

@rolandhegedus
Copy link

@rolandhegedus rolandhegedus commented Mar 14, 2015

Client.prototype.connect = function() {
    if (!this.connection) {
        this.connection = this.socket.connect(config.host + ':' + config.port);
    }
    else {
        if (!this.connection.connected) {
            this.connection.connect();
        }
    }
};
@nasasutisna
Copy link

@nasasutisna nasasutisna commented Mar 1, 2019

io.connect(null,{'force new connection':true});

this solve my problem, thank you mr. milami

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

Successfully merging a pull request may close this issue.

None yet