在onStart和onPause之间切换后的多个Scarlet回调



通过使用Kotlin,我创建了一个网络模块,代码在下面

val networkModule = module {
......
single {
return@single AndroidLifecycle.ofApplicationForeground(androidApplication())
}
// Scarlet WebSocket library for Tinder
single {
return@single Scarlet.Builder()
.webSocketFactory(get<OkHttpClient>().newWebSocketFactory(Wss))
.lifecycle(get())  // I can create my own lifeCycle and combineWith(), for example accounts.isEmpty
.backoffStrategy(LinearBackoffStrategy(5000))
.addMessageAdapterFactory(GsonMessageAdapter.Factory(get()))
.addStreamAdapterFactory(CoroutinesStreamAdapterFactory())
.build()
.create(FlowSocketApi::class.java)
}
}
val lifecycleModule = module {
single {
return@single AppLifeCycleObserver()
}
}

我还有一个Observer类,它已注册到应用程序生命周期中。

class AppLifeCycleObserver :
LifecycleObserver, KoinComponent {
private val flowEventsObserver: LiveData<WebSocket.Event> =
flowSocketService.observeEvents().consumeAsFlow().asLiveData()
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
flowEventsObserver.observeForever { event ->
when(event) {
is WebSocket.Event.OnConnectionOpened<*> -> {
println("WebSocket.Event.OnConnectionOpened")
println(event)
}
is WebSocket.Event.OnMessageReceived -> {
println("WebSocket.Event.OnMessageReceived")
println(event.message.toString())
}
is WebSocket.Event.OnConnectionClosing -> {
println("WebSocket.Event.OnConnectionClosing")
println(event)
}
is WebSocket.Event.OnConnectionClosed -> {
println("WebSocket.Event.OnConnectionClosed")
println(event)
}
is WebSocket.Event.OnConnectionFailed -> {
println("WebSocket.Event.OnConnectionFailed")
println(event)
}
}
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {

}
}

当然是在应用程序类中初始化的

class MyApplication : Application() {
private val appLifeCycleObserver: AppLifeCycleObserver by inject()
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApplication)
modules(
arrayListOf(networkModule,databaseModule,managersModule,repositoryModule,lifecycleModule)
)
}
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifeCycleObserver)
}

当应用程序启动时,Scarlet会创建一个连接,并触发onEnterForeground。因此LiveData输出OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@6795837)

之后,我按下主页按钮,应用程序进入后台,因此Scarlet必须取消当前的套接字连接。然而,在重新启动应用程序后,应用程序进入前台启动,Scarlet必须重新打开连接,但问题是,在我的Logcat中,我看到的是打印的两倍

OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@b87493b)
OnConnectionOpened(webSocket=okhttp3.internal.ws.RealWebSocket@b87493b)

当我继续做同样的事情时,onConnectionOpened会相乘。。。!!出了什么问题??

@Gabiele Petrioli

private val flowEventsObserver: LiveData<FlowSocketResponse> =
flowSocketService.observeEvents().consumeAsFlow().asLiveData()
private lateinit var mObserver: Observer<WebSocket.Event>

所以EnterForeground就像

mObserver = Observer<WebSocket.Event> { it ->
it?.let {
flowSocketHandler.webSocketEventResolver(it)
}
}
flowEventsObserver.observeForever(mObserver)

onEnterBackground是

flowEventsObserver.removeObserver(mObserver)

然而,当我暂停并重新启动应用程序时,我会遇到一个例外,即实时数据只能观察一次或几次。

最新更新