Modern Apps git

← Modern-Apps.git

findfamily: fix connection not working

Commit
02b995f248a41a7c9e5c25b81533a5f3f7d72fe4
Author
Vayun Mathur <[email protected]>
When
2026-08-14 05:05 UTC (3 months ago)
Parents
7dd7386

Diff

findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.ktindex a8f26aa470..90e4a4f828 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt@@ -16,8 +16,10 @@ import com.vayunmathur.library.network.webSocket import com.vayunmathur.library.util.DataStoreUtils import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job+import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch@@ -155,6 +157,14 @@ object Networking {      private const val GETKEY_TIMEOUT_MS = 5_000L +    /**+     * Keepalive ping cadence. Mobile NATs/proxies drop idle sockets within a few+     * minutes, and a half-open socket is otherwise only noticed on the next write;+     * a periodic ping keeps the connection alive and surfaces breakage fast so the+     * server's fan-outs land and queued backlog drains promptly on reconnect.+     */+    private const val PING_INTERVAL_MS = 30_000L+     @Volatile private var wsSession: WsSession? = null     private var liveJob: Job? = null @@ -215,11 +225,31 @@ object Networking {                         send(sub)                         backoff = 1000L                         Log.d(TAG, "live WS connected, subscribed+registered as ${userid.toULong()} bundleLen=${bundle.size}")-                        incoming.collect { frame ->-                            when (frame) {-                                is WebSocketClient.WsFrame.Binary ->-                                    dispatchLiveFrame(frame.bytes, onLocations, onUwb)-                                else -> Unit+                        // Keepalive: ping on a timer while the socket loop runs, and tear the+                        // socket down if a ping write fails (half-open) so we reconnect and+                        // drain instead of silently missing fan-outs. Cancelled when the+                        // incoming loop ends (below), which ends this coroutineScope.+                        coroutineScope {+                            val pinger = launch {+                                while (isActive) {+                                    delay(PING_INTERVAL_MS)+                                    runCatching { ping() }.onFailure {+                                        if (it is CancellationException) throw it+                                        Log.w(TAG, "keepalive ping failed; closing to reconnect", it)+                                        runCatching { close() }+                                    }+                                }+                            }+                            try {+                                incoming.collect { frame ->+                                    when (frame) {+                                        is WebSocketClient.WsFrame.Binary ->+                                            dispatchLiveFrame(frame.bytes, onLocations, onUwb)+                                        else -> Unit+                                    }+                                }+                            } finally {+                                pinger.cancel()                             }                         }                     }
library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.ktindex 27c9ba239d..4845046125 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/WebSocketClient.kt@@ -384,6 +384,9 @@ class WsSession internal constructor(      suspend fun send(text: String) = client.send(text)     suspend fun send(bytes: ByteArray) = client.send(bytes)++    /** Sends a PING frame. Callers that need keepalive run their own timer loop over this. */+    suspend fun ping() = client.ping()     suspend fun close() = client.close() }