socketio / socket.io-client-swift

Other
5.22k stars 844 forks source link

Multi connection after reconnected #605

Open anngdev opened 7 years ago

anngdev commented 7 years ago
NSURL* url = [[NSURL alloc] initWithString:AISocketManagerHost];
self.socket =  [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @NO, @"forceNew":@YES, @"reconnectWait":@1}];
[self.socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"socket connected");
}];
[self.socket on:@"reconnect" callback:^(NSArray* data, SocketAckEmitter* ack) {
    NSLog(@"socket disconnected, reconnecting...");
}];
[self.socket connect];

When I disconnected it for about 30s (turn off/on wifi/3g on iPhone), it will reconnect with 2 more connections.

2017-01-12 15:44:02.505 Playground[1047:360336] socket connected
2017-01-12 15:44:07.391 Playground[1047:360336] socket disconnected, reconnecting...
2017-01-12 15:44:41.994 Playground[1047:360336] socket connected
2017-01-12 15:44:42.368 Playground[1047:360336] socket connected
2017-01-12 15:44:43.757 Playground[1047:360336] socket connected

If I set @"reconnectWait":@2, it will be 1 more connections.

I'm using version 8.2.0

anngdev commented 7 years ago

It happens only on heroku server. it works fine on local server. My server code is:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
  socket.on('chatmessage', function(msg){
    console.log('listening on *:'+process.env.PORT || 5000);
    io.emit('chatmessage', msg);
  });
});

http.listen(process.env.PORT || 5000, function(){
  console.log('listening on *:'+process.env.PORT || 5000);
});

https://morning-peak-67693.herokuapp.com/

codeninja88 commented 7 years ago

Where are you issuing your connect function... i had this issue a few weeks ago and fixed it by moving my connect function to the App Delegate so that it is called only once.

anngdev commented 7 years ago

[self.socket connect] is called only once, I was add breakpoint and I sure about that. The problem is it only happen on heroku (with the same server code) :(

ipandage commented 7 years ago

@codeninja88 how did you solve it , can you be more specific?