@@ -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 {
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user