Tinder / Scarlet

A Retrofit inspired WebSocket client for Kotlin, Java, and Android
Other
3.24k stars 239 forks source link

Creating a second Service causes socket reconnection #189

Open tobioyelekan opened 3 years ago

tobioyelekan commented 3 years ago

Creating a second Service like this makes the socket reconnect, thereby opening multiple connections

@Provides
@Singleton
fun provideAuthService(scarlet: Scarlet): Service1 {
    return scarlet.create(Service1::class.java)
}

@Provides
@Singleton
fun providesUserService(scarlet: Scarlet): Service2 {
    return scarlet.create(Service2::class.java)
}

@Provides
@Singleton
fun providesScarlet(): Scarlet {
    val client = OkHttpClient.Builder()
        .connectTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)

    return Scarlet.Builder()
        .webSocketFactory(client.build().newWebSocketFactory(baseUrl))
        .addMessageAdapterFactory(
            WebSocketMessageAdapter.Factory()
        )
        .addStreamAdapterFactory(FlowStreamAdapter.Factory)
        //.lifecycle(AndroidLifecycle.ofApplicationForeground(application))
        .backoffStrategy(BACKOFF_STRATEGY) //remove
        .build()
}

Is this an intended behavior? because having just one service with a long list of @Send and @Receive doesn't look neat.

boronov commented 1 year ago

Сould you find a solution?