Scarlet icon indicating copy to clipboard operation
Scarlet copied to clipboard

WebSocket not retrying connection when server closes connection

Open martinmihalic opened this issue 5 years ago • 4 comments

I'm dealing with a problem that my server could be unstable at the time and he could silently reboot itself. The issue is that my socket is not retrying connection after the server closes it. Is that expected behaviour? These are the logs that I get:

2019-06-26 00:43:28.715 32195-32420/(ConnectionManagerImpl.kt:40)#onServerReadyChange DebugTree: Dispatching web socket connection event > OnConnectionClosing(shutdownReason=ShutdownReason(code=1001, reason=The web application is stopping))

2019-06-26 00:43:28.728 32195-32420/(ConnectionManagerImpl.kt:40)#onServerReadyChange DebugTree: Dispatching web socket connection event > OnConnectionClosed(shutdownReason=ShutdownReason(code=1001, reason=The web application is stopping))

My implementation of Scarlet configuration is this:

inline fun <reified T> createWebSocketService(okHttpClient: OkHttpClient, url: String, objectMapper: ObjectMapper, application: Application): T {
    val protocol = OkHttpWebSocket(
            okHttpClient,
            OkHttpWebSocket.SimpleRequestFactory(
                    { Request.Builder().url(url).build() },
                    { ShutdownReason.GRACEFUL }
            )
    )
    val configuration = Scarlet.Configuration(
            messageAdapterFactories = listOf(JacksonMessageAdapter.Factory(objectMapper)),
            streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()),
            backoffStrategy = LinearBackoffStrategy(5000),
            lifecycle = AndroidLifecycle.ofApplicationForeground(application)
    )
    return Scarlet(protocol, configuration).create(T::class.java)
}

martinmihalic avatar Jun 25 '19 22:06 martinmihalic

any updates about this?

muhammadFawzy avatar Nov 14 '19 13:11 muhammadFawzy

Figured out that this might be an issue with older versions of Android, i am working on an app that uses Android 4.4 the websocket doesn't reconnect, but in newer versions it works fine, not sure how to go about this.

axelbau24 avatar Nov 15 '19 15:11 axelbau24

@axelbau24 i don't think it's related to android version as i have android 8 and 9 and when server is down for 1 minute for any reason websocket doesn't trying to reconnect.

muhammadFawzy avatar Nov 15 '19 17:11 muhammadFawzy

Could this issue have the same underlying cause as https://github.com/Tinder/Scarlet/issues/65, which is retrying being stopped by a WebSocket.Event.OnConnectionFailed event? There is a fix proposed in https://github.com/Tinder/Scarlet/pull/66 that could be good to try.

I've been listening for these events and closing and reopening a Scarlet LifecycleRegistry instance to work around this.

TTransmit avatar Apr 15 '20 11:04 TTransmit