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
Labels
bug Something isn't working unable to reproduce

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 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

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 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 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

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 commented Oct 4, 2011

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

@skrat
Copy link

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 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 commented Nov 1, 2011

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

@skrat
Copy link

skrat commented Nov 1, 2011

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

@milani
Copy link

milani commented Nov 1, 2011

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

@skrat
Copy link

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 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 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 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 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 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 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 commented Nov 27, 2011

ah, great news :-)

@cacois
Copy link

cacois commented Nov 28, 2011

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

@baradas
Copy link

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

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 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-- 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

It works for me! Thanks @mrjoes.

@Plasma
Copy link

Plasma commented Jun 25, 2013

Thanks @milani

@danjunger
Copy link

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 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 as completed Jun 6, 2014
@pitambar
Copy link

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

@peteruithoven
Copy link

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

@maxcodefranco
Copy link

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

@rolandhegedus
Copy link

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

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
Labels
bug Something isn't working unable to reproduce
Projects
None yet
Development

No branches or pull requests