Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always reconnect #368

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
)
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
<string name="missed_messages">Missed messages</string>
<string name="grouped_notification_text">New Messages</string>
<string name="websocket_listening">Connected</string>
<string name="websocket_could_not_connect">Could not connect</string>
<string name="websocket_init">Initializing</string>
<string name="versions">gotify/android v%1$s; gotify/server v%2$s</string>
<string name="advanced_settings">Advanced Settings</string>
Expand Down Expand Up @@ -112,7 +111,7 @@
<string name="action_dialog_button_open">Open</string>
<string name="action_dialog_button_cancel">Cancel</string>

<string name="websocket_not_connected">Not connected</string>
<string name="websocket_error">Error %s (see logs)</string>
<string name="websocket_reconnect">Trying to reconnect</string>
<plurals name="websocket_retry_interval">
<item quantity="one">in %d minute</item>
Expand Down
Loading