Use static var to track SSE status

This commit is contained in:
sim 2023-03-26 18:34:46 +02:00
parent 881e442ac6
commit bea744db18
2 changed files with 15 additions and 6 deletions

View File

@ -30,8 +30,6 @@ class SSEListener(val context: Context) : EventSourceListener() {
it.release()
}
}
started = false
pinged = false
try {
Log.d(TAG, "onOpen: " + response.code)
} catch (e: Exception) {
@ -85,7 +83,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
Log.d(TAG, "onClosed: $eventSource")
eventSource.cancel()
if (!shouldRestart()) return
FailureHandler.newFail(context, eventSource, started, pinged)
FailureHandler.newFail(context, eventSource)
clearVars()
RestartWorker.run(context, delay = 0)
}
@ -102,9 +101,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
if (!RestartNetworkCallback.hasInternet) {
Log.d(TAG, "No Internet: do not restart")
FailureHandler.once(eventSource)
clearVars()
return
}
FailureHandler.newFail(context, eventSource, started, pinged)
FailureHandler.newFail(context, eventSource)
clearVars()
val delay = when (FailureHandler.nFails) {
1 -> 2 // 2sec
2 -> 5 // 5sec
@ -133,6 +134,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
return true
}
private fun clearVars() {
started = false
pinged = false
}
companion object {
var lastEventDate: Calendar? = null
var keepalive = 900

View File

@ -3,6 +3,7 @@ package org.unifiedpush.distributor.nextpush.services
import android.content.Context
import android.util.Log
import okhttp3.sse.EventSource
import org.unifiedpush.distributor.nextpush.api.SSEListener
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification
@ -29,7 +30,7 @@ object FailureHandler {
nFailsBeforePing = 0
}
fun newFail(context: Context, eventSource: EventSource?, started: Boolean, pinged: Boolean) {
fun newFail(context: Context, eventSource: EventSource?) {
Log.d(TAG, "newFail/Eventsource: $eventSource")
// ignore fails from a possible old eventSource
// if we are already reconnected
@ -39,7 +40,8 @@ object FailureHandler {
if (nFails == 2) {
showWarningNotification(context)
}
if (started && !pinged) {
if (SSEListener.started && !SSEListener.pinged) {
Log.d(TAG, "The service has started and it has never received a ping.")
nFailsBeforePing++
if (nFailsBeforePing == 3) {
showNoPingNotification(context)
@ -70,6 +72,7 @@ object FailureHandler {
fun clearFails() {
nFails = 0
nFailsBeforePing = 0
eventSource = null
}