iOS StarScream, WebSocket sometimes does not reconnect on app foreground

1k views Asked by At

This has been a real brain scratcher for us

We know iOS closes WebSocket connections for apps in the background after a certain period of time.

We noticed that every time this happens, and the app comes back to Foreground, our socket delegate receives the websocketDidDisconnect message. And when things are working, we quickly get the websocketDidConnect message.

The assumption here is that StarScream is handling the automatic reconnect on foreground

However, in a small percentage of cases, clients only receive websocketDidDisconnect on foreground, but never receive websocketDidConnect message.

Furthermore, we attempt to manually determine this disconnect on foreground:

private var serverSocket: WebSocket?

init() {
    NotificationCenter.default.addObserver(self, selector: #selector(reconnect), name: UIApplication.willEnterForegroundNotification, object: nil)        
}

@objc func reconnect() {
    LogManagerProvider.shared.info("Reconnecting WebSocket")
    LogManagerProvider.shared.info((serverSocket?.isConnected == true) ? "Before Reconnect Attempt: Already Connected" : "Before Reconnect Attempt: Disconnected")
    self.serverSocket?.connect()
}

In the case where the socket is back up on foreground, we see serverSocket.isConnected == true But in the case where it's failed, we see serverSocket.isConnected == false, and the connect call doesn't re-connect the socket as we'd expect, either

Has anybody seen this before, or know what could be the issue?

0

There are 0 answers