Browse Source

message type

wr_202303
zsw 4 months ago
parent
commit
77a759f06a
  1. 61
      lib/im/SocketClient.dart
  2. 38
      lib/im/database/message.dart

61
lib/im/SocketClient.dart

@ -11,6 +11,8 @@ import 'package:huixiang/im/database/message.dart';
import 'package:huixiang/im/out/auth.pb.dart';
import 'package:huixiang/im/out/message.pb.dart';
import 'package:huixiang/main.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SocketClient {
@ -42,20 +44,19 @@ class SocketClient {
MsgData dataResult = MsgData.fromBuffer(proto.body);
print('收到来自:${dataResult.from},消息内容: ${utf8.decode(dataResult.data)} ');
Map<String, dynamic> messageMap = createMessage(userId, dataResult.data, msgType: dataResult.type.value, userId: dataResult.from);
if (callbacks[dataResult.from] != null) {
messageMap["state"] = 1;
}
hxDatabase.insert(messageMap).then((value) {
messageMap["id"] = value;
Message message = Message.fromJson(messageMap);
receiveInsertMessage(dataResult).then((messageMap) {
if (callbacks[dataResult.from] != null) {
callbacks[dataResult.from].call(message); /// user conversation callback
messageMap["state"] = 1;
}
callbacks[userId]?.call(message); /// user self conversation list callback
hxDatabase.insert(messageMap).then((value) {
messageMap["id"] = value;
Message message = Message.fromJson(messageMap);
if (callbacks[dataResult.from] != null) {
callbacks[dataResult.from].call(message); /// user conversation callback
}
callbacks[userId]?.call(message); /// user self conversation list callback
});
});
}, onError: (Object error, StackTrace stackTrace) {
debugPrint("socket-listen-error: $error, stackTrace: $stackTrace");
showDebugToast("socket-listen-error: $error, stackTrace: $stackTrace");
@ -75,6 +76,42 @@ class SocketClient {
});
}
Future<Map<String, dynamic>> receiveInsertMessage(MsgData dataResult) async {
String content = "";
String attach = "";
Uint8List dataBytes = dataResult.data;
MsgType type = MsgType.values[dataResult.type.value];
if (type == MsgType.TEXT) {
content = utf8.decode(dataBytes);
} else if (type == MsgType.IMAGE || type == MsgType.VIDEO) {
Map<String, dynamic> result = await ImageGallerySaver.saveImage(
dataBytes,
isReturnImagePathOfIOS: true,
);
bool isSuccess = result["isSuccess"] != null && result["isSuccess"];
if (isSuccess) {
attach = result["filePath"];
}
} else if (type == MsgType.AUDIO) {
Directory dir = await getTemporaryDirectory();
File file = File("${dir.path}/hx_${DateTime.now().millisecondsSinceEpoch}.wav");
try {
IOSink ioSink = file.openWrite();
ioSink.write(dataBytes);
ioSink.close();
attach = file.path;
} catch (e) {
debugPrint("${file.path} write fail");
}
} else {
content = "暂不支持的消息格式";
}
Map<String, dynamic> messageMap = createMessage(userId, content, attach: attach, msgType: type.value, fromId: dataResult.from);
return messageMap;
}
showDebugToast(text) {
if (kDebugMode) {
SmartDialog.showToast(text, alignment: Alignment.center);
@ -172,7 +209,7 @@ class SocketClient {
}
Future<Message> sendMessage(String toId, String content) async {
Map<String, dynamic> message = createSendMessage(toId, content, userId: userId);
Map<String, dynamic> message = createMessage(toId, content, fromId: userId);
message["state"] = 1;
int id = await hxDatabase.insert(message).catchError((error) {
debugPrint("insertMessage: $error");

38
lib/im/database/message.dart

@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
@ -7,6 +8,7 @@ import 'package:flutter/cupertino.dart';
import 'package:huixiang/im/out/message.pb.dart';
import 'package:huixiang/im/out/message.pbenum.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
class Message {
int id;
@ -64,10 +66,10 @@ class Message {
};
}
createSendMessage(var toId, String content, {String attach, int msgType, userId, replyId}) {
createMessage(var toId, String content, {String attach, int msgType, fromId, replyId}) {
return <String, dynamic>{
"conversationId": conversationId(userId, toId),
"fromId": userId,
"conversationId": conversationId(fromId, toId),
"fromId": fromId,
"toId": toId,
"replyId": replyId,
"content": content,
@ -79,36 +81,6 @@ createSendMessage(var toId, String content, {String attach, int msgType, userId,
};
}
createMessage(var toId, Uint8List dataBytes, {String attach, int msgType, userId, replyId}) {
String content = "";
MsgType type = MsgType.values[msgType];
if (type == MsgType.TEXT) {
content = utf8.decode(dataBytes);
}
if (type == MsgType.IMAGE || type == MsgType.AUDIO || type == MsgType.VIDEO) {
Map<String, dynamic> result = ImageGallerySaver.saveImage(
dataBytes,
isReturnImagePathOfIOS: true,
);
bool isSuccess = result["isSuccess"] != null && result["isSuccess"];
if (isSuccess) {
attach = result["filePath"];
}
}
return <String, dynamic>{
"conversationId": conversationId(userId, toId),
"fromId": userId,
"toId": toId,
"replyId": replyId,
"content": content,
"attach": attach,
"msgType": msgType ?? 1,
"time": "${DateTime.now().millisecondsSinceEpoch}",
"state": 0,
"isDelete": 0
};
}
conversationId(tid, fid) {
num itid = num.parse(tid);

Loading…
Cancel
Save