From 27835c8c7fcc606187d3e76e63bb9dbe7c7b9337 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Fri, 2 Aug 2024 12:41:41 +0200 Subject: [PATCH] fix: always reconnect --- .../gotify/service/WebSocketConnection.kt | 25 ++++--------------- .../github/gotify/service/WebSocketService.kt | 13 +++------- app/src/main/res/values/strings.xml | 3 +-- 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/app/src/main/kotlin/com/github/gotify/service/WebSocketConnection.kt b/app/src/main/kotlin/com/github/gotify/service/WebSocketConnection.kt index b3e809c2..5c55b0be 100644 --- a/app/src/main/kotlin/com/github/gotify/service/WebSocketConnection.kt +++ b/app/src/main/kotlin/com/github/gotify/service/WebSocketConnection.kt @@ -38,8 +38,7 @@ internal class WebSocketConnection( private lateinit var onMessageCallback: (Message) -> Unit private lateinit var onClose: Runnable private lateinit var onOpen: Runnable - private lateinit var onBadRequest: BadRequestRunnable - private lateinit var onNetworkFailure: OnNetworkFailureRunnable + private lateinit var onFailure: OnNetworkFailureRunnable private lateinit var onReconnected: Runnable private var state: State? = null @@ -71,14 +70,8 @@ internal class WebSocketConnection( } @Synchronized - fun onBadRequest(onBadRequest: BadRequestRunnable): WebSocketConnection { - this.onBadRequest = onBadRequest - return this - } - - @Synchronized - fun onNetworkFailure(onNetworkFailure: OnNetworkFailureRunnable): WebSocketConnection { - this.onNetworkFailure = onNetworkFailure + fun onFailure(onFailure: OnNetworkFailureRunnable): WebSocketConnection { + this.onFailure = onFailure return this } @@ -192,15 +185,11 @@ internal class WebSocketConnection( Logger.error(t) { "WebSocket($id): failure $code Message: $message" } syncExec(id) { closed() - if (response != null && response.code >= 400 && response.code <= 499) { - onBadRequest.execute(message) - return@syncExec - } errorCount++ val minutes = (errorCount * 2 - 1).coerceAtMost(20) - onNetworkFailure.execute(minutes) + onFailure.execute(response?.message ?: "unreachable", minutes) scheduleReconnect(TimeUnit.MINUTES.toSeconds(minutes.toLong())) } super.onFailure(webSocket, t, response) @@ -214,12 +203,8 @@ internal class WebSocketConnection( } } - internal fun interface BadRequestRunnable { - fun execute(message: String) - } - internal fun interface OnNetworkFailureRunnable { - fun execute(minutes: Int) + fun execute(status: String, minutes: Int) } internal enum class State { diff --git a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt index 7ad0078c..3bfc6984 100644 --- a/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt +++ b/app/src/main/kotlin/com/github/gotify/service/WebSocketService.kt @@ -111,8 +111,7 @@ internal class WebSocketService : Service() { ) .onOpen { onOpen() } .onClose { onClose() } - .onBadRequest { message -> onBadRequest(message) } - .onNetworkFailure { minutes -> onNetworkFailure(minutes) } + .onFailure { status, minutes -> onFailure(status, minutes) } .onMessage { message -> onMessage(message) } .onReconnected { notifyMissedNotifications() } .start() @@ -179,16 +178,12 @@ internal class WebSocketService : Service() { connection!!.scheduleReconnect(15) } - private fun onBadRequest(message: String) { - showForegroundNotification(getString(R.string.websocket_could_not_connect), message) - } - - private fun onNetworkFailure(minutes: Int) { - val status = getString(R.string.websocket_not_connected) + private fun onFailure(status: String, minutes: Int) { + val title = getString(R.string.websocket_error, status) val intervalUnit = resources .getQuantityString(R.plurals.websocket_retry_interval, minutes, minutes) showForegroundNotification( - status, + title, "${getString(R.string.websocket_reconnect)} $intervalUnit" ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 206fcbd3..d0e4e2c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -64,7 +64,6 @@ Missed messages New Messages Connected - Could not connect Initializing gotify/android v%1$s; gotify/server v%2$s Advanced Settings @@ -112,7 +111,7 @@ Open Cancel - Not connected + Error %s (see logs) Trying to reconnect in %d minute