Add timeout to SSE Listener to restart earlier the connection

This commit is contained in:
sim 2024-10-31 14:38:42 +00:00
parent c2a27b5233
commit f6edb178dd
3 changed files with 15 additions and 1 deletions

View File

@ -61,7 +61,7 @@ dependencies {
implementation('com.google.android.material:material:1.11.0')
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.coordinatorlayout:coordinatorlayout:1.2.0")
implementation('com.squareup.okhttp3:okhttp-sse:4.12.0')
implementation('com.squareup.okhttp3:okhttp-sse:5.0.0.SSEPATCH1-SNAPSHOT')
implementation("com.github.nextcloud:Android-SingleSignOn:0.8.1")
implementation("com.squareup.retrofit2:retrofit:$retrofitVersion")
implementation("com.squareup.retrofit2:converter-gson:$retrofitVersion")

View File

@ -30,6 +30,8 @@ import kotlin.concurrent.schedule
class SSEListener(val context: Context) : EventSourceListener() {
override var timeout: Long? = AppCompanion.keepalive.get().toLong() + TIMEOUT_TOLERANCE
override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource)
StartingTimer.scheduleNewTimer(context, eventSource)
@ -63,6 +65,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
val message = Gson().fromJson(data, SSEResponse::class.java)
message.keepalive.let {
AppCompanion.keepalive.set(it)
timeout = it.toLong() + TIMEOUT_TOLERANCE
Log.d(TAG, "New keepalive: $it")
if (it < 25) {
LowKeepAliveNotification(context, it).showSingle()
@ -193,4 +196,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
startingTimer.getAndSet(null)?.cancel()
}
}
private companion object {
private const val TIMEOUT_TOLERANCE = 5L // 5 seconds
}
}

View File

@ -30,6 +30,13 @@ allprojects {
includeModule 'com.github.nextcloud', 'Android-SingleSignOn'
}
}
maven {
url "https://codeberg.org/NextPush/maven/raw/branch/main/okhttp-sse/releases/"
content {
includeModule "com.squareup.okhttp3", "okhttp-sse"
}
}
mavenLocal()
}
apply plugin: "org.jlleitschuh.gradle.ktlint"
}