From e990652981074ecf358a394352c443c856ffe1f6 Mon Sep 17 00:00:00 2001 From: lvmeng <848755140@qq.com> Date: Wed, 6 May 2026 09:03:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(scale):=20=E4=BF=AE=E5=A4=8D=E5=AD=90?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=96=AD=E7=BA=BF=E5=90=8E=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E6=85=A2=E5=8F=8A=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E7=AB=9E?= =?UTF-8?q?=E6=80=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - onDeviceDisconnected 时同步清除 UDP 缓存,使子设备 5s 内即可重连,不再等待指数退避 - ScaleWebSocketServer 用 connectionCount 计数器替代 connections.isNullOrEmpty(),消除 onClose 竞态导致子设备 UI 状态不更新的问题 --- .../com/shuwei/dish/match/scale/ScaleServiceManager.kt | 6 +++++- .../com/shuwei/dish/match/scale/ScaleWebSocketServer.kt | 9 +++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt b/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt index 82ee0c8..ed8cc65 100644 --- a/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt +++ b/app/src/main/java/com/shuwei/dish/match/scale/ScaleServiceManager.kt @@ -156,7 +156,11 @@ object ScaleServiceManager { val client = ScaleWebSocketClient().also { it.onScaleData = { data -> aggregator?.onRemoteScaleData(data) } it.onScaleEvent = { event -> _onScaleEvent?.invoke(event) } - it.onDeviceDisconnected = { remoteId -> aggregator?.removeDevice(remoteId) } + it.onDeviceDisconnected = { remoteId -> + aggregator?.removeDevice(remoteId) + // 清除 UDP 缓存,确保子设备下次广播时能重新触发 onDeviceFound,立即重连而非等待指数退避 + udpReceiver?.removeDevice(remoteId) + } it.onDeviceConnected = { remoteId -> // 子设备连接成功时,从 Room 读取全量配置并单独推送给该设备 CoroutineScope(Dispatchers.IO).launch { diff --git a/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt b/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt index e6f64d9..1578439 100644 --- a/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt +++ b/app/src/main/java/com/shuwei/dish/match/scale/ScaleWebSocketServer.kt @@ -117,7 +117,11 @@ class ScaleWebSocketServer(private val deviceId: String, private val context: Co private inner class InternalServer(port: Int) : WebSocketServer(InetSocketAddress(port)) { + /** 当前活跃连接计数,用于替代 connections.isNullOrEmpty() 避免竞态 */ + private var connectionCount = 0 + override fun onOpen(conn: WebSocket, handshake: ClientHandshake) { + connectionCount++ Log.d(TAG, "主设备已连接: ${conn.remoteSocketAddress}") onConnectionChanged?.invoke(true) // 新客户端连接后,立即推送所有秤的最新数据 @@ -132,8 +136,9 @@ class ScaleWebSocketServer(private val deviceId: String, private val context: Co override fun onClose(conn: WebSocket, code: Int, reason: String, remote: Boolean) { Log.d(TAG, "主设备已断开: ${conn.remoteSocketAddress}, reason=$reason") - // 若已无任何连接,通知子设备 UI 更新为未连接状态 - if (server?.connections.isNullOrEmpty()) { + // 用计数器判断是否还有活跃连接,避免依赖 connections 集合的移除时机导致竞态 + if (--connectionCount <= 0) { + connectionCount = 0 onConnectionChanged?.invoke(false) } }