Report driver Reverb connection status
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.gson.Gson
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
@@ -28,6 +29,8 @@ class DriverLiveSyncClient(
|
||||
private val started = AtomicBoolean(false)
|
||||
private var webSocket: WebSocket? = null
|
||||
private var driverId: String? = null
|
||||
private var socketId: String? = null
|
||||
private var heartbeatJob: Job? = null
|
||||
|
||||
fun start(driverId: String) {
|
||||
if (BuildConfig.REVERB_APP_KEY.isBlank()) return
|
||||
@@ -46,11 +49,15 @@ class DriverLiveSyncClient(
|
||||
}
|
||||
|
||||
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
|
||||
stopHeartbeat()
|
||||
reportRealtimeStatus("disconnected", reason.takeIf { it.isNotBlank() })
|
||||
started.set(false)
|
||||
scheduleReconnect()
|
||||
}
|
||||
|
||||
override fun onFailure(webSocket: WebSocket, t: Throwable, response: okhttp3.Response?) {
|
||||
stopHeartbeat()
|
||||
reportRealtimeStatus("error", t.message ?: response?.message)
|
||||
started.set(false)
|
||||
scheduleReconnect()
|
||||
}
|
||||
@@ -60,9 +67,12 @@ class DriverLiveSyncClient(
|
||||
|
||||
fun stop() {
|
||||
started.set(false)
|
||||
stopHeartbeat()
|
||||
reportRealtimeStatus("disconnected", "client_stop")
|
||||
webSocket?.close(1000, "logout")
|
||||
webSocket = null
|
||||
driverId = null
|
||||
socketId = null
|
||||
}
|
||||
|
||||
fun close() {
|
||||
@@ -76,9 +86,17 @@ class DriverLiveSyncClient(
|
||||
|
||||
when (event) {
|
||||
"pusher:connection_established" -> {
|
||||
val socketId = root.dataObject()?.string("socket_id") ?: return
|
||||
subscribe(socket, socketId)
|
||||
onConnected()
|
||||
socketId = root.dataObject()?.string("socket_id") ?: return
|
||||
subscribe(socket, socketId ?: return)
|
||||
}
|
||||
"pusher_internal:subscription_succeeded" -> {
|
||||
val channel = root.string("channel")
|
||||
val id = driverId ?: return
|
||||
if (channel == "private-driver-mobile.$id") {
|
||||
reportRealtimeStatus("connected")
|
||||
startHeartbeat()
|
||||
onConnected()
|
||||
}
|
||||
}
|
||||
"DriverMobileSyncHint" -> parseHint(root.dataObject())?.let(onHint)
|
||||
}
|
||||
@@ -132,4 +150,27 @@ class DriverLiveSyncClient(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startHeartbeat() {
|
||||
heartbeatJob?.cancel()
|
||||
heartbeatJob = scope.launch {
|
||||
while (started.get()) {
|
||||
reportRealtimeStatus("connected")
|
||||
delay(30_000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopHeartbeat() {
|
||||
heartbeatJob?.cancel()
|
||||
heartbeatJob = null
|
||||
}
|
||||
|
||||
private fun reportRealtimeStatus(status: String, error: String? = null) {
|
||||
scope.launch {
|
||||
runCatching {
|
||||
repository.storeRealtimeStatus(status, socketId, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user