mirror of
https://github.com/WuKongIM/WuKongIMFlutterSDK
synced 2025-05-28 22:52:20 +00:00
Optimize connection
This commit is contained in:
parent
4cfefa89e5
commit
c1753a5c84
@ -45,4 +45,6 @@
|
||||
### 1.2.2
|
||||
* Modifying the issue of reconnecting disconnected objects without destroying them
|
||||
### 1.2.3
|
||||
* Optimize message queries
|
||||
* Optimize message queries
|
||||
### 1.2.4
|
||||
* Optimize connection
|
@ -9,7 +9,7 @@
|
||||
#### 安装
|
||||
```
|
||||
dependencies:
|
||||
wukongimfluttersdk: ^1.2.3
|
||||
wukongimfluttersdk: ^1.2.4
|
||||
```
|
||||
#### 引入
|
||||
```dart
|
||||
|
@ -76,7 +76,7 @@ class ListViewShowDataState extends State<ListViewShowData> {
|
||||
if (isAdd) {
|
||||
msgList.add(UIConversation(msg));
|
||||
}
|
||||
if (isEnd) {
|
||||
if (isEnd && mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -45,4 +45,4 @@ SPEC CHECKSUMS:
|
||||
|
||||
PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367
|
||||
|
||||
COCOAPODS: 1.14.3
|
||||
COCOAPODS: 1.11.2
|
||||
|
@ -259,7 +259,7 @@
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 0920;
|
||||
LastUpgradeCheck = 1300;
|
||||
LastUpgradeCheck = 1430;
|
||||
ORGANIZATIONNAME = "";
|
||||
TargetAttributes = {
|
||||
331C80D4294CF70F00263BE5 = {
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1300"
|
||||
LastUpgradeVersion = "1430"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
@ -507,7 +507,7 @@ packages:
|
||||
path: ".."
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.2.1"
|
||||
version: "1.2.3"
|
||||
x25519:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -23,7 +23,6 @@ class _WKSocket {
|
||||
Socket? _socket; // 将 _socket 声明为可空类型
|
||||
bool _isListening = false;
|
||||
static _WKSocket? _instance;
|
||||
|
||||
_WKSocket._internal(this._socket);
|
||||
|
||||
factory _WKSocket.newSocket(Socket socket) {
|
||||
@ -32,16 +31,21 @@ class _WKSocket {
|
||||
}
|
||||
|
||||
void close() {
|
||||
_socket?.close();
|
||||
_isListening = false;
|
||||
_socket = null; // 现在可以将 _socket 设置为 null
|
||||
_instance = null;
|
||||
try {
|
||||
_socket?.destroy();
|
||||
} finally {
|
||||
_socket = null; // 现在可以将 _socket 设置为 null
|
||||
}
|
||||
}
|
||||
|
||||
void send(Uint8List data) {
|
||||
try {
|
||||
_socket?.add(data); // 使用安全调用操作符
|
||||
_socket?.flush();
|
||||
if (_socket?.remotePort != null) {
|
||||
_socket?.add(data); // 使用安全调用操作符
|
||||
_socket?.flush();
|
||||
}
|
||||
} catch (e) {
|
||||
Logs.debug('发送消息错误$e');
|
||||
}
|
||||
@ -52,9 +56,8 @@ class _WKSocket {
|
||||
_socket!.listen(onData, onError: (err) {
|
||||
Logs.debug('socket断开了${err.toString()}');
|
||||
}, onDone: () {
|
||||
Logs.debug('socketonDone');
|
||||
error();
|
||||
close(); // 关闭和重置 Socket 连接
|
||||
error();
|
||||
});
|
||||
_isListening = true;
|
||||
}
|
||||
@ -123,8 +126,13 @@ class WKConnectionManager {
|
||||
if (_socket != null) {
|
||||
_socket!.close();
|
||||
}
|
||||
// if (isLogout) {
|
||||
// }
|
||||
if (isLogout) {
|
||||
// _isLogout = true;
|
||||
WKIM.shared.options.uid = '';
|
||||
WKIM.shared.options.token = '';
|
||||
WKIM.shared.messageManager.updateSendingMsgFail();
|
||||
WKDBHelper.shared.close();
|
||||
}
|
||||
_closeAll();
|
||||
}
|
||||
|
||||
@ -157,10 +165,10 @@ class WKConnectionManager {
|
||||
// _decodePacket(data);
|
||||
}, () {
|
||||
if (_isLogout) {
|
||||
Logs.debug("登出了");
|
||||
return;
|
||||
}
|
||||
isReconnection = true;
|
||||
Logs.error('发送消息失败$_isLogout');
|
||||
// isReconnection = true;
|
||||
Future.delayed(Duration(milliseconds: reconnMilliseconds), () {
|
||||
connect();
|
||||
});
|
||||
@ -171,8 +179,6 @@ class WKConnectionManager {
|
||||
|
||||
_connectFail(error) {
|
||||
// _socket?.close();
|
||||
Logs.error('连接失败:${error.toString()}');
|
||||
print(error);
|
||||
Future.delayed(Duration(milliseconds: reconnMilliseconds), () {
|
||||
connect();
|
||||
});
|
||||
@ -250,7 +256,6 @@ class WKConnectionManager {
|
||||
_decodePacket(Uint8List data) {
|
||||
var packet = WKIM.shared.options.proto.decode(data);
|
||||
Logs.debug('解码出包->$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) {
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user