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

Reconnection doesn't work

Open kvaDrug opened this issue 7 years ago • 12 comments

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?

kvaDrug avatar May 10 '18 21:05 kvaDrug

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.

kvaDrug avatar May 13 '18 07:05 kvaDrug

I have the same problem

wongzigii avatar May 14 '18 04:05 wongzigii

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

nuclearace avatar Jun 01 '18 13:06 nuclearace

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

nickngsr avatar Jun 18 '18 02:06 nickngsr

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

<head><title>502 Bad Gateway</title></head>

<body bgcolor="white">

<center><h1>502 Bad Gateway</h1></center>

<hr><center>nginx</center>

</body>

</html>


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

<head><title>502 Bad Gateway</title></head>

<body bgcolor="white">

<center><h1>502 Bad Gateway</h1></center>

<hr><center>nginx</center>

</body>

</html>


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

<head><title>502 Bad Gateway</title></head>

<body bgcolor="white">

<center><h1>502 Bad Gateway</h1></center>

<hr><center>nginx</center>

</body>

</html>


2018-06-18 18:29:05.562381+0100 four[2661:2628085] LOG SocketIOClient{/ipad}: Handling event: error with data: ["Got unknown error from server <html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r\n"]
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

nickngsr avatar Jun 18 '18 17:06 nickngsr

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

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

prgorasiya avatar Jul 03 '18 08:07 prgorasiya

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

nickngsr avatar Jul 05 '18 15:07 nickngsr

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.

luzianscherrer avatar Jul 31 '18 09:07 luzianscherrer

I don't believe this should be closed. We are seeing the same behavior on v16.0.1

willtrking avatar Feb 26 '21 07:02 willtrking

I don't believe this should be closed. We are seeing the same behavior on v16.0.1

Can you reopen this issue by yourself or my action is needed?

kvaDrug avatar Mar 15 '21 13:03 kvaDrug

@willtrking I can conform this behavior on v16.0.1 too.

bowenbowensun avatar Apr 11 '21 01:04 bowenbowensun

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

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

Solved with this solution, thank you very much.

pajarft avatar Mar 01 '22 05:03 pajarft