From c1753a5c849d2d10b8cd371b18a6ff5eca9a0ada Mon Sep 17 00:00:00 2001 From: SL Date: Mon, 11 Dec 2023 14:09:33 +0800 Subject: [PATCH] Optimize connection --- CHANGELOG.md | 4 +- README.md | 2 +- example/lib/home.dart | 2 +- example/lib/http.dart | 1 + example/lib/main.dart | 17 +++++++ example/macos/Podfile.lock | 2 +- .../macos/Runner.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- example/pubspec.lock | 2 +- lib/manager/connect_manager.dart | 50 +++++++++++-------- pubspec.yaml | 2 +- 11 files changed, 56 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f5c1ff..b6056af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,4 +45,6 @@ ### 1.2.2 * Modifying the issue of reconnecting disconnected objects without destroying them ### 1.2.3 - * Optimize message queries \ No newline at end of file + * Optimize message queries +### 1.2.4 + * Optimize connection \ No newline at end of file diff --git a/README.md b/README.md index 1a7293b..ac89691 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ #### 安装 ``` dependencies: - wukongimfluttersdk: ^1.2.3 + wukongimfluttersdk: ^1.2.4 ``` #### 引入 ```dart diff --git a/example/lib/home.dart b/example/lib/home.dart index aae6ade..9721460 100644 --- a/example/lib/home.dart +++ b/example/lib/home.dart @@ -76,7 +76,7 @@ class ListViewShowDataState extends State { if (isAdd) { msgList.add(UIConversation(msg)); } - if (isEnd) { + if (isEnd && mounted) { setState(() {}); } }); diff --git a/example/lib/http.dart b/example/lib/http.dart index 08e3ebc..a920e45 100644 --- a/example/lib/http.dart +++ b/example/lib/http.dart @@ -32,6 +32,7 @@ class HttpUtils { static syncConversation(String lastSsgSeqs, int msgCount, int version, Function(WKSyncConversation) back) async { + print("同步最近会话的参数${version}"); final dio = Dio(); final response = await dio.post('$apiURL/conversation/sync', data: { "uid": UserInfo.uid, // 当前登录用户uid diff --git a/example/lib/main.dart b/example/lib/main.dart index c3cf656..e3b0b44 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -4,11 +4,28 @@ import 'package:example/const.dart'; import 'package:example/http.dart'; import 'package:example/im.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:wukongimfluttersdk/wkim.dart'; import 'home.dart'; void main() { runApp(const MyApp()); + SystemChannels.lifecycle.setMessageHandler((msg) async { + // msg是个字符串,是下面的值 + // AppLifecycleState.resumed + // AppLifecycleState.inactive + // AppLifecycleState.paused + // AppLifecycleState.detached + if (msg == "AppLifecycleState.paused") { + print("应用在后台"); + WKIM.shared.connectionManager.disconnect(false); + } else if (msg == "AppLifecycleState.resumed") { + print("应用在前台"); + WKIM.shared.connectionManager.connect(); + } + return msg; + }); } class MyApp extends StatelessWidget { diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index 6daf42c..c8788f4 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -45,4 +45,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367 -COCOAPODS: 1.14.3 +COCOAPODS: 1.11.2 diff --git a/example/macos/Runner.xcodeproj/project.pbxproj b/example/macos/Runner.xcodeproj/project.pbxproj index 877bd76..3b5b226 100644 --- a/example/macos/Runner.xcodeproj/project.pbxproj +++ b/example/macos/Runner.xcodeproj/project.pbxproj @@ -259,7 +259,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0920; - LastUpgradeCheck = 1300; + LastUpgradeCheck = 1430; ORGANIZATIONNAME = ""; TargetAttributes = { 331C80D4294CF70F00263BE5 = { diff --git a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index 8fedab6..397f3d3 100644 --- a/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ $packet'); - Logs.debug('解码出包类型->${packet.header.packetType}'); if (packet.header.packetType == PacketType.connack) { var connackPacket = packet as ConnackPacket; if (connackPacket.reasonCode == 1) { @@ -283,8 +288,6 @@ class WKConnectionManager { if (_sendingMsgMap.containsKey(sendack.clientSeq)) { _sendingMsgMap[sendack.clientSeq]!.isCanResend = false; } - Logs.debug( - '发送消息ack 发送状态:${sendack.reasonCode},消息编号:${sendack.clientSeq}'); } else if (packet.header.packetType == PacketType.disconnect) { _closeAll(); setConnectionStatus(WKConnectStatus.kicked, 'ReasonConnectKick'); @@ -294,14 +297,17 @@ class WKConnectionManager { } _closeAll() { - _isLogout = true; - WKIM.shared.options.uid = ''; - WKIM.shared.options.token = ''; - WKIM.shared.messageManager.updateSendingMsgFail(); + // _isLogout = true; + // WKIM.shared.options.uid = ''; + // WKIM.shared.options.token = ''; + // WKIM.shared.messageManager.updateSendingMsgFail(); _stopCheckNetworkTimer(); _stopHeartTimer(); - _socket!.close(); - WKDBHelper.shared.close(); + if (_socket != null) { + _socket!.close(); + } + + // WKDBHelper.shared.close(); } _sendReceAckPacket(BigInt messageID, int messageSeq) { diff --git a/pubspec.yaml b/pubspec.yaml index e93eef5..1871aea 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.2.3 +version: 1.2.4 homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK environment: