socketio / socket.io-client-swift

Other
5.22k stars 845 forks source link

Multiple reconnection attempts triggers multiple disconnection events #48

Closed ahallora closed 9 years ago

ahallora commented 9 years ago

First of all, I love the stability gained from using this new wonderful library! It's quicker and more reliable than the unofficial framework I used before. :+1:

However, i'm experiencing a glitch which causes multiple reconnectionAttempts to trigger a disconnect-event on the server for the failed attempts. It is best described with an example.

The case is this: 1) SwiftIO is connected to a node socket.io server - it's all good 2) I stop the server, wait 10 seconds, and start the server again 3) SwiftIO starts reconnecting - in this example it triggers reconnectAttempt 3 times before reconnecting 4) The server acknowledges all 3 attempts by triggering the connection-event on the server 3 times 5) After approx. 25 seconds the disconnect-event is triggered on the server for the 2 failed reconnects

My iOS code (in obj-c sorry):

    NSMutableDictionary *socketParams = [[NSMutableDictionary alloc] init];
    socketParams[@"username"] = @"username";

    NSMutableDictionary *socketOptions = [[NSMutableDictionary alloc] init];
    socketOptions[@"reconnectWait"] = @5;
    socketOptions[@"timeout"] = @3; // undocumented - just trying my luck :) 
    socketOptions[@"connect timeout"] = @3; // undocumented - just trying my luck :) 
    socketOptions[@"connectTimeout"] = @3; // undocumented - just trying my luck :)     
    socketOptions[@"forceWebsockets"] = @TRUE;

    SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:1337" options:socketOptions];

    [socket connectWithParams:(socketParams)];

    [socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) {
        NSLog(@"socket connected (or reconnected)");
    }];

    [socket on: @"reconnectAttempt" callback:^(NSArray* data, void (^ack)(NSArray*)) {
        NSLog(@"Reconnect attempt");
    }];

    [socket on: @"reconnect" callback:^(NSArray* data, void (^ack)(NSArray*)) {
        NSLog(@"socket starting reconnect");
    }];

My server code looks like this:

io.on('connection', function (socket) {
    console.log('connection triggered');

    socket.on('disconnect', function () {
        console.log('disconnect triggered');
    })
});

Is it working as intended? I don't think that a disconnect event should be triggered on failed connection attempts - e.g. there's no connection ergo no disconnect to invoke. :smile:

nuclearace commented 9 years ago

I'm having difficulty reproducing, because once you turn off the server, the client should immediately try and reconnect once and fail. It'll then wait 5 seconds and fail again. Then it'll wait 5 more seconds and try again, which depending on whether you've started the server by now will either fail and then succeed the next try, or succeed now.

nuclearace commented 9 years ago

Also there isn't any undocumented options, they're all listed here https://github.com/socketio/socket.io-client-swift#options