update sync channel msgs

This commit is contained in:
SL 2023-08-19 21:10:18 +08:00
parent 6a64a7f145
commit a19656e45e
11 changed files with 44 additions and 33 deletions

View File

@ -3,4 +3,6 @@
### 1.0.1 ### 1.0.1
* update connection * update connection
### 1.0.2 ### 1.0.2
* Reconnect when handling connection errors * Reconnect when handling connection errors
### 1.0.3
* update sync channel msgs

View File

@ -10,7 +10,7 @@
#### 安装 #### 安装
``` ```
dependencies: dependencies:
wukongimfluttersdk: ^1.0.2 wukongimfluttersdk: ^1.0.3
``` ```
#### 引入 #### 引入
```dart ```dart

View File

@ -102,6 +102,8 @@ class ChatListDataState extends State<ChatList> {
Future.delayed(const Duration(milliseconds: 300), () { Future.delayed(const Duration(milliseconds: 300), () {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent); _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
}); });
}, () {
print('消息同步中');
}); });
} }

View File

@ -79,7 +79,7 @@ class HttpUtils {
int pullMode, int pullMode,
Function(WKSyncChannelMsg) back) async { Function(WKSyncChannelMsg) back) async {
final dio = Dio(); final dio = Dio();
print('请求参数${UserInfo.uid},$channelID'); print('请求参数${startMsgSeq},$endMsgSeq');
final response = await dio.post('$apiURL/channel/messagesync', data: { final response = await dio.post('$apiURL/channel/messagesync', data: {
"login_uid": UserInfo.uid, // uid "login_uid": UserInfo.uid, // uid
"channel_id": channelID, // ID "channel_id": channelID, // ID
@ -118,7 +118,6 @@ class HttpUtils {
msg.channelID = json['channel_id']; msg.channelID = json['channel_id'];
// msg.payload = json['payload']; // msg.payload = json['payload'];
String payload = json['payload']; String payload = json['payload'];
// print('消息发送着:${msg.from_uid},${msg.channel_id}');
try { try {
msg.payload = jsonDecode(utf8.decode(base64Decode(payload))); msg.payload = jsonDecode(utf8.decode(base64Decode(payload)));
// print('查询的消息${msg.payload}'); // print('查询的消息${msg.payload}');

View File

@ -499,7 +499,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "1.0.2" version: "1.0.3"
x25519: x25519:
dependency: transitive dependency: transitive
description: description:

View File

@ -28,7 +28,7 @@ class WKDBConst {
msg.messageSeq = readInt(data, 'message_seq'); msg.messageSeq = readInt(data, 'message_seq');
msg.clientSeq = readInt(data, 'client_seq'); msg.clientSeq = readInt(data, 'client_seq');
msg.timestamp = readInt(data, 'timestamp'); msg.timestamp = readInt(data, 'timestamp');
msg.fromUID = readString(data, ' from_uid'); msg.fromUID = readString(data, 'from_uid');
msg.channelID = readString(data, 'channel_id'); msg.channelID = readString(data, 'channel_id');
msg.channelType = readInt(data, 'channel_type'); msg.channelType = readInt(data, 'channel_type');
msg.contentType = readInt(data, 'type'); msg.contentType = readInt(data, 'type');

View File

@ -260,11 +260,11 @@ class MessaggeDB {
bool contain, bool contain,
int pullMode, int pullMode,
int limit, int limit,
final Function(List<WKMsg>) iGetOrSyncHistoryMsgBack) async { final Function(List<WKMsg>) iGetOrSyncHistoryMsgBack,
final Function() syncBack) async {
// //
List<WKMsg> list = await getMessages( List<WKMsg> list = await getMessages(
channelId, channelType, oldestOrderSeq, contain, pullMode, limit); channelId, channelType, oldestOrderSeq, contain, pullMode, limit);
// //
List<WKMsg> tempList = []; List<WKMsg> tempList = [];
for (int i = 0, size = list.length; i < size; i++) { for (int i = 0, size = list.length; i < size; i++) {
@ -383,9 +383,9 @@ class MessaggeDB {
if (isSyncMsg && if (isSyncMsg &&
(startMsgSeq != endMsgSeq || (startMsgSeq == 0 && endMsgSeq == 0)) && (startMsgSeq != endMsgSeq || (startMsgSeq == 0 && endMsgSeq == 0)) &&
requestCount < 5) { requestCount < 5) {
// if (requestCount == 0) { if (requestCount == 0) {
// iGetOrSyncHistoryMsgBack.onSyncing(); syncBack();
// } }
// //
requestCount++; requestCount++;
WKIM.shared.messageManager.setSyncChannelMsgListener( WKIM.shared.messageManager.setSyncChannelMsgListener(
@ -395,7 +395,7 @@ class MessaggeDB {
syncChannelMsg.messages != null && syncChannelMsg.messages != null &&
syncChannelMsg.messages!.isNotEmpty) { syncChannelMsg.messages!.isNotEmpty) {
getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq, getOrSyncHistoryMessages(channelId, channelType, oldestOrderSeq,
contain, pullMode, limit, iGetOrSyncHistoryMsgBack); contain, pullMode, limit, iGetOrSyncHistoryMsgBack, syncBack);
} else { } else {
requestCount = 0; requestCount = 0;
iGetOrSyncHistoryMsgBack(list); iGetOrSyncHistoryMsgBack(list);

View File

@ -44,6 +44,7 @@ class _WKSocket {
class WKConnectionManager { class WKConnectionManager {
bool isReconnection = false; bool isReconnection = false;
final int reconnMilliseconds = 1500;
Timer? heartTimer; Timer? heartTimer;
Timer? checkNetworkTimer; Timer? checkNetworkTimer;
final heartIntervalSecond = const Duration(seconds: 60); final heartIntervalSecond = const Duration(seconds: 60);
@ -99,7 +100,7 @@ class WKConnectionManager {
} }
_socketConnect(String addr) { _socketConnect(String addr) {
Logs.info("addr--->$addr"); Logs.info("连接地址--->$addr");
var addrs = addr.split(":"); var addrs = addr.split(":");
var host = addrs[0]; var host = addrs[0];
var port = addrs[1]; var port = addrs[1];
@ -110,10 +111,8 @@ class WKConnectionManager {
_socket = _WKSocket.newSocket(socket); _socket = _WKSocket.newSocket(socket);
_connectSuccess(); _connectSuccess();
}).catchError((err) { }).catchError((err) {
Logs.info("失败");
_connectFail(err); _connectFail(err);
}).onError((err, stackTrace) { }).onError((err, stackTrace) {
Logs.info("错误");
_connectFail(err); _connectFail(err);
}); });
} catch (e) { } catch (e) {
@ -121,7 +120,7 @@ class WKConnectionManager {
} }
} }
// socket // socket
_connectSuccess() { _connectSuccess() {
// //
_socket?.listen((Uint8List data) { _socket?.listen((Uint8List data) {
@ -129,7 +128,8 @@ class WKConnectionManager {
// _decodePacket(data); // _decodePacket(data);
}, () { }, () {
isReconnection = true; isReconnection = true;
Future.delayed(const Duration(milliseconds: 1500), () { Logs.error('发送消息失败');
Future.delayed(Duration(milliseconds: reconnMilliseconds), () {
connect(); connect();
}); });
}); });
@ -138,7 +138,10 @@ class WKConnectionManager {
} }
_connectFail(error) { _connectFail(error) {
Logs.error(error); Logs.error('连接失败:${error.toString()}');
Future.delayed(Duration(milliseconds: reconnMilliseconds), () {
connect();
});
} }
testCutData(Uint8List data) { testCutData(Uint8List data) {
@ -272,7 +275,6 @@ class WKConnectionManager {
} }
_sendConnectPacket() { _sendConnectPacket() {
Logs.info("发送连接包");
var connectPacket = ConnectPacket( var connectPacket = ConnectPacket(
uid: WKIM.shared.options.uid!, uid: WKIM.shared.options.uid!,
token: WKIM.shared.options.token!, token: WKIM.shared.options.token!,
@ -393,24 +395,23 @@ class WKConnectionManager {
msg.messageContent = WKIM.shared.messageManager msg.messageContent = WKIM.shared.messageManager
.getMessageModel(msg.contentType, contentJson); .getMessageModel(msg.contentType, contentJson);
WKIM.shared.messageManager.parsingMsg(msg); WKIM.shared.messageManager.parsingMsg(msg);
WKIM.shared.messageManager.saveMsg(msg);
if (msg.isDeleted == 0 && if (msg.isDeleted == 0 &&
!msg.header.noPersist && !msg.header.noPersist &&
msg.contentType != WkMessageContentType.insideMsg) { msg.contentType != WkMessageContentType.insideMsg) {
List<WKMsg> list = []; int row = await WKIM.shared.messageManager.saveMsg(msg);
list.add(msg); msg.clientSeq = row;
WKIM.shared.messageManager.pushNewMsg(list);
WKUIConversationMsg? uiMsg = WKUIConversationMsg? uiMsg =
await WKIM.shared.conversationManager.saveWithLiMMsg(msg); await WKIM.shared.conversationManager.saveWithLiMMsg(msg);
if (uiMsg != null) { if (uiMsg != null) {
Logs.info('刷新最近会话');
WKIM.shared.conversationManager.setRefreshMsg(uiMsg, true); WKIM.shared.conversationManager.setRefreshMsg(uiMsg, true);
} }
} else { } else {
Logs.debug( Logs.debug(
'消息不能存库:is_deleted=${msg.isDeleted},no_persist=${msg.header.noPersist},content_type:${msg.contentType}'); '消息不能存库:is_deleted=${msg.isDeleted},no_persist=${msg.header.noPersist},content_type:${msg.contentType}');
} }
List<WKMsg> list = [];
list.add(msg);
WKIM.shared.messageManager.pushNewMsg(list);
} }
int _isDeletedMsg(dynamic jsonObject) { int _isDeletedMsg(dynamic jsonObject) {

View File

@ -80,8 +80,8 @@ class WKMessageManager {
return MessaggeDB.shared.queryWithClientMsgNo(clientMsgNo); return MessaggeDB.shared.queryWithClientMsgNo(clientMsgNo);
} }
saveMsg(WKMsg msg) { Future<int> saveMsg(WKMsg msg) async {
MessaggeDB.shared.insert(msg); return await MessaggeDB.shared.insert(msg);
} }
String generateClientMsgNo() { String generateClientMsgNo() {
@ -181,7 +181,8 @@ class WKMessageManager {
int pullMode, int pullMode,
int limit, int limit,
int aroundMsgOrderSeq, int aroundMsgOrderSeq,
final Function(List<WKMsg>) iGetOrSyncHistoryMsgBack) async { final Function(List<WKMsg>) iGetOrSyncHistoryMsgBack,
final Function() syncBack) async {
if (aroundMsgOrderSeq != 0) { if (aroundMsgOrderSeq != 0) {
int maxMsgSeq = await getMaxMessageSeq(channelId, channelType); int maxMsgSeq = await getMaxMessageSeq(channelId, channelType);
int aroundMsgSeq = getOrNearbyMsgSeq(aroundMsgOrderSeq); int aroundMsgSeq = getOrNearbyMsgSeq(aroundMsgOrderSeq);
@ -222,8 +223,15 @@ class WKMessageManager {
contain = true; contain = true;
} }
} }
MessaggeDB.shared.getOrSyncHistoryMessages(channelId, channelType, MessaggeDB.shared.getOrSyncHistoryMessages(
oldestOrderSeq, contain, pullMode, limit, iGetOrSyncHistoryMsgBack); channelId,
channelType,
oldestOrderSeq,
contain,
pullMode,
limit,
iGetOrSyncHistoryMsgBack,
syncBack);
} }
int getOrNearbyMsgSeq(int orderSeq) { int getOrNearbyMsgSeq(int orderSeq) {
@ -319,7 +327,7 @@ class WKMessageManager {
dynamic json = wkMsg.messageContent!.encodeJson(); dynamic json = wkMsg.messageContent!.encodeJson();
json['type'] = wkMsg.contentType; json['type'] = wkMsg.contentType;
wkMsg.content = jsonEncode(json); wkMsg.content = jsonEncode(json);
int row = await MessaggeDB.shared.insert(wkMsg); int row = await saveMsg(wkMsg);
wkMsg.clientSeq = row; wkMsg.clientSeq = row;
WKIM.shared.messageManager.setOnMsgInserted(wkMsg); WKIM.shared.messageManager.setOnMsgInserted(wkMsg);
if (wkMsg.messageContent is WKMediaMessageContent) { if (wkMsg.messageContent is WKMediaMessageContent) {

View File

@ -80,7 +80,6 @@ class Proto {
} }
Uint8List encodeConnect(ConnectPacket packet) { Uint8List encodeConnect(ConnectPacket packet) {
Logs.info("连接包信息${packet.toString()}");
WriteData write = WriteData(); WriteData write = WriteData();
write.writeUint8(packet.version); write.writeUint8(packet.version);
write.writeUint8(packet.deviceFlag); write.writeUint8(packet.deviceFlag);

View File

@ -15,7 +15,7 @@ description: wukong IM flutter sdk
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # 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 # 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. # of the product and file versions while build-number is used as the build suffix.
version: 1.0.2 version: 1.0.3
homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK homepage: https://github.com/WuKongIM/WuKongIMFlutterSDK
environment: environment: