fix(scale): 修复 mDNS 抖动导致子设备永久无法重连的问题

This commit is contained in:
2026-04-14 18:25:20 +08:00
parent 69dc77f06e
commit 94e49eb344
2 changed files with 6 additions and 6 deletions
@@ -111,16 +111,16 @@ object ScaleServiceManager {
client.connect(remoteId, host, port)
}
// mDNS 发现:发现子设备 → 建立 WebSocket 连接;设备离线 → 断开连接并清除数据
// mDNS 发现:发现子设备 → 建立 WebSocket 连接;设备离线 → 仅清除 UDP 缓存,WebSocket 层自行重连
mdnsDiscovery = MdnsDiscoveryManager(context).also { discovery ->
discovery.onDeviceFound = { remoteId, host, port ->
onDeviceFound(remoteId, host, port)
}
discovery.onDeviceLost = { remoteId ->
Log.d(TAG, "子设备离线 $remoteId, 断开连接并清除数据")
client.disconnect(remoteId)
aggregator?.removeDevice(remoteId)
// mDNS 抖动不永久断开,WebSocket 层会持续重连直到网络恢复
// 仅清除 UDP 缓存,确保 UDP 重新发现时能再次触发 onDeviceFound
udpReceiver?.removeDevice(remoteId)
Log.d(TAG, "子设备 mDNS 离线: $remoteId, 保持 WebSocket 重连")
}
discovery.startDiscovery()
}
@@ -100,10 +100,10 @@ class ScaleWebSocketClient {
/**
* 指数退避重连
* 入口不拦截 removedDevices,仅在延迟任务执行前检查,
* 确保 disconnect() 后已入队的任务不执行,但新断线事件始终能进入重连流程
*/
private fun scheduleReconnect(deviceId: String, host: String, port: Int) {
if (removedDevices.contains(deviceId)) return
val delay = reconnectDelays[deviceId] ?: RECONNECT_BASE_MS
Log.d(TAG, "将在 ${delay}ms 后重连子设备: $deviceId")