Socket.io-client-swift: Reconnection doesn't work

Created on 10 May 2018  路  8Comments  路  Source: socketio/socket.io-client-swift

I use Socket IO v13.1.1 on iOS app with server (apache nginx, Socket IO v2.0.4). We test what will happen if server reboots.

  1. iOS app is connected to the server
  2. Then the server is rebooted
  3. iOS app tries to reconnect

iOS library constantly emits reconnectAttempt event (default config). Why it doesn't reconnects?
Is iOS app supposed to perform some custom actions?

Most helpful comment

Disconnecting manager first works for me.
My code is as follow

socket?.onAny { (data) in
            if data.event == "reconnectAttempt" {
                self.socketManager?.disconnect()
                self.connectSocket()
            }
        }

All 8 comments

I was able to fix my problem by disabling built-in reconnection:
manager = SocketManager(socketURL: serverURL, config: [.reconnects(false)])
And then I created a Timer that will periodically check if the socket is connected and reconnects if necessary.

I have the same problem

Sorry for missing this. Does the logs spit out something about an unknown session id?

I have the same issue.

If I listen to the 'reconnectAttempt' event, which is still fired as expected, and in that I call disconnect on the SocketManager, the reconnect attempt will work. This doesn't seem to fire the 'disconnect' event.

It may be worth noting that the socket in question is on a non-root namespace

```
2018-06-18 18:29:05.424781+0100 four[2661:2628085] LOG SocketManager: Starting reconnect
2018-06-18 18:29:05.425002+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: statusChange with data: [connecting]
2018-06-18 18:29:05.425093+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: reconnect with data: ["Socket Disconnected"]
Reconnect
2018-06-18 18:29:05.425401+0100 four[2661:2628085] LOG SocketManager: Trying to reconnect
2018-06-18 18:29:05.425491+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: reconnectAttempt with data: [-1]
Reconnect Attempt
2018-06-18 18:29:05.437001+0100 four[2661:2628085] LOG SocketManager: Adding engine
2018-06-18 18:29:05.437254+0100 four[2661:2628112] LOG SocketEngine: Engine is being released
2018-06-18 18:29:05.437425+0100 four[2661:2628251] LOG SocketEngine: Starting engine. Server: https://xxxx
2018-06-18 18:29:05.437449+0100 four[2661:2628251] LOG SocketEngine: Handshaking
2018-06-18 18:29:05.437613+0100 four[2661:2628251] LOG SocketEnginePolling: Doing polling GET https://xxxx/socket.io/?transport=polling&b64=1
2018-06-18 18:29:05.438333+0100 four[2661:2628123] TIC Read Status [1:0x0]: 1:57
2018-06-18 18:29:05.438352+0100 four[2661:2628123] TIC Read Status [1:0x0]: 1:57
2018-06-18 18:29:05.561286+0100 four[2661:2628112] LOG SocketEnginePolling: Got polling response
2018-06-18 18:29:05.561445+0100 four[2661:2628112] LOG SocketEnginePolling: Got poll message:

502 Bad Gateway

502 Bad Gateway


nginx

2018-06-18 18:29:05.561648+0100 four[2661:2628112] LOG SocketEngine: Got message:

502 Bad Gateway

502 Bad Gateway


nginx

2018-06-18 18:29:05.562229+0100 four[2661:2628085] ERROR SocketManager: Got unknown error from server

502 Bad Gateway

502 Bad Gateway


nginx

2018-06-18 18:29:05.562381+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: error with data: ["Got unknown error from server rn502 Bad Gatewayrnrn

502 Bad Gateway

rn
nginx
rnrnrn"]
2018-06-18 18:29:08.630287+0100 four[2661:2628085] LOG SocketManager: Trying to reconnect
2018-06-18 18:29:08.630664+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: reconnectAttempt with data: [-2]
Reconnect Attempt
2018-06-18 18:29:08.631158+0100 four[2661:2628085] LOG SocketManager: Tried connecting an already active socket
2018-06-18 18:29:11.930020+0100 four[2661:2628085] LOG SocketManager: Trying to reconnect
2018-06-18 18:29:11.930204+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: reconnectAttempt with data: [-3]
Reconnect Attempt
````

The socket manager is a property of a singleton class that is never disposed of

Disconnecting manager first works for me.
My code is as follow

socket?.onAny { (data) in
            if data.event == "reconnectAttempt" {
                self.socketManager?.disconnect()
                self.connectSocket()
            }
        }

Yeah I got it to work doing the same thing, #909 has a proposed fix that seems to make sense

I can confirm the same thing. After adding [self.manager disconnect]; before every [self.socket disconnect]; in my code, things are working as expected again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gaetanm picture gaetanm  路  4Comments

heisian picture heisian  路  4Comments

janiduw picture janiduw  路  5Comments

zevarito picture zevarito  路  3Comments

MD04-TanTan picture MD04-TanTan  路  5Comments