From c0bb596c6a4281e82c4708f7f4d3299b6af26075 Mon Sep 17 00:00:00 2001 From: SL Date: Mon, 21 Apr 2025 16:11:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=9C=A8=E6=9C=AA?= =?UTF-8?q?=E6=94=B6=E5=88=B0=E6=9C=8D=E5=8A=A1=E7=AB=AF=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=97=B6=E4=B8=BB=E5=8A=A8=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 +++- lib/manager/connect_manager.dart | 10 ++++++++++ pubspec.lock | 16 ---------------- pubspec.yaml | 3 +-- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e648c4..bdda252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -123,4 +123,6 @@ ### 1.6.1 * fix: 修复网络切换时有时无法连接问题 ### 1.6.2 - * fix: 修改数据库解码错误数据导致oom \ No newline at end of file + * fix: 修改数据库解码错误数据导致oom +### 1.6.3 + * fix: 优化在未收到服务端心跳消息时主动断开重连 \ No newline at end of file diff --git a/lib/manager/connect_manager.dart b/lib/manager/connect_manager.dart index ec76ae0..bdc5f2e 100644 --- a/lib/manager/connect_manager.dart +++ b/lib/manager/connect_manager.dart @@ -81,6 +81,7 @@ class WKConnectionManager { Timer? checkNetworkTimer; final heartIntervalSecond = const Duration(seconds: 60); final checkNetworkSecond = const Duration(seconds: 1); + int unReceivePongCount = 0; final LinkedHashMap _sendingMsgMap = LinkedHashMap(); HashMap? _connectionListenerMap; _WKSocket? _socket; @@ -229,6 +230,7 @@ class WKConnectionManager { var packetType = b >> 4; if (PacketType.values[(b >> 4)] == PacketType.pong) { Logs.debug('pong'); + unReceivePongCount = 0; Uint8List bytes = lastMsgBytes.sublist(1, lastMsgBytes.length); _cacheData = lastMsgBytes = bytes; } else { @@ -273,6 +275,7 @@ class WKConnectionManager { _decodePacket(Uint8List data) { var packet = WKIM.shared.options.proto.decode(data); Logs.debug('解码出包->$packet'); + unReceivePongCount = 0; if (packet.header.packetType == PacketType.connack) { var connackPacket = packet as ConnackPacket; if (connackPacket.reasonCode == 1) { @@ -414,7 +417,14 @@ class WKConnectionManager { _startHeartTimer() { _stopHeartTimer(); heartTimer = Timer.periodic(heartIntervalSecond, (timer) { + if (unReceivePongCount > 0) { + Logs.debug('心跳包未收到pong,重连中...'); + isReconnection = false; + connect(); + return; + } Logs.info('ping...'); + unReceivePongCount++; _sendPacket(PingPacket()); }); } diff --git a/pubspec.lock b/pubspec.lock index ea2067e..bfd5252 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -549,22 +549,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" - web_socket: - dependency: transitive - description: - name: web_socket - sha256: "3c12d96c0c9a4eec095246debcea7b86c0324f22df69893d538fcc6f1b8cce83" - url: "https://pub.dev" - source: hosted - version: "0.1.6" - web_socket_channel: - dependency: "direct main" - description: - name: web_socket_channel - sha256: "0b8e2457400d8a859b7b2030786835a28a8e80836ef64402abef392ff4f1d0e5" - url: "https://pub.dev" - source: hosted - version: "3.0.2" x25519: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index d6643a7..b9c1729 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ description: wukong IM flutter sdk # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.6.2 +version: 1.6.3 homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK environment: @@ -41,7 +41,6 @@ dependencies: hex: ^0.2.0 crypto: ^3.0.6 uuid: ^4.3.3 - web_socket_channel: ^3.0.1 dio: ^5.3.2 shared_preferences: ^2.2.0 sqflite: ^2.4.1