Tinder / Scarlet

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

With default lifecycle config, SocketIoClient is not connecting #44

Open hannta opened 5 years ago

hannta commented 5 years ago

If I do not specify lifecycle for Scarlet.Configuration, SocketIoClient is not connecting.

My dagger module which creates Scarlet

@Module
class MykModule {
    @Singleton
    @Provides
    fun provideScarletConfiguration(app: Application): Scarlet.Configuration {
        return Scarlet.Configuration(
            lifecycle = AndroidLifecycle.ofApplicationForeground(app), // With out this, Scarlet is not connectin
            messageAdapterFactories = listOf(MoshiMessageAdapter.Factory()),
            streamAdapterFactories = listOf(RxJava2StreamAdapterFactory()),
            )
    }

    @Singleton
    @Provides
    fun provideScarlet(config: Scarlet.Configuration): Scarlet {
        val client = SocketIoClient({ "https://my.api" })
        return Scarlet(client, config)
    }

    @Singleton
    @Provides
    fun provideMyApi(scarlet: Scarlet): MyApi {
        return scarlet.create<MyApi>()
    }

    @Singleton
    @Provides
    fun providePingTopic(scarlet: Scarlet, config: Scarlet.Configuration): PingTopic {
        return scarlet.child(SocketIoEventName("ping"), config).create<PingTopic>()
    }
}

I try to keep my connection alive even when the app is in background, so I can not use lifecycle AndroidLifecycle.ofApplicationForeground(app)

Im using Scarlet 0.2.1-alpha4

zhxnlai commented 5 years ago

@hannta Thanks for bring this up! I will look into this issue

zhxnlai commented 5 years ago

@hannta Just released 0.2.4-alpha1 to address this issue. Could you let me know if works?

iadcialim commented 2 years ago

@zhxnlai How bout if app is closed, it means websocket connection is also closed right? I am coming from the view of a push notification, where it is still delivered even if the app is closed.