socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

Connect -> Disconnect -> Connect doesn't work

Open JonathanJonathanJonathan opened this issue 15 years ago • 33 comments

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');

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');
});

aarong avatar Jul 27 '11 13:07 aarong

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

Gorokhov avatar Aug 18 '11 11:08 Gorokhov

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!

milani avatar Aug 29 '11 15:08 milani

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.

skrat avatar Sep 21 '11 14:09 skrat

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

respectTheCode avatar Oct 03 '11 19:10 respectTheCode

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

milani avatar Oct 04 '11 08:10 milani

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

skrat avatar Oct 04 '11 09:10 skrat

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.

mrjoes avatar Nov 01 '11 12:11 mrjoes

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

milani avatar Nov 01 '11 13:11 milani

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

skrat avatar Nov 01 '11 13:11 skrat

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

milani avatar Nov 01 '11 13:11 milani

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

skrat avatar Nov 01 '11 13:11 skrat

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

Not very good idea to be honest.

mrjoes avatar Nov 01 '11 13:11 mrjoes

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

milani avatar Nov 01 '11 13:11 milani

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.

mrjoes avatar Nov 01 '11 13:11 mrjoes

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

amnjdm avatar Nov 03 '11 06:11 amnjdm

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.

cacois avatar Nov 27 '11 15:11 cacois

It appears like there will be a major update soon because of this pull request: https://github.com/LearnBoost/socket.io-client/pull/343 @cacois Try using socket.socket.reconnect() rather than socket.reconnect()

amnjdm avatar Nov 27 '11 20:11 amnjdm

ah, great news :-)

mrjoes avatar Nov 27 '11 21:11 mrjoes

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

cacois avatar Nov 28 '11 02:11 cacois