|
|
|
@ -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,8 +44,7 @@ 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); |
|
|
|
|
|
|
|
|
|
receiveInsertMessage(dataResult).then((messageMap) { |
|
|
|
|
if (callbacks[dataResult.from] != null) { |
|
|
|
|
messageMap["state"] = 1; |
|
|
|
|
} |
|
|
|
@ -55,7 +56,7 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
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); |
|
|
|
@ -171,8 +208,23 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Future<Message> sendMessage(String toId, String content) async { |
|
|
|
|
Map<String, dynamic> message = createSendMessage(toId, content, userId: userId); |
|
|
|
|
Future<Message> sendMessage(String toId, String content, {String attach, int msgType, replyId}) async { |
|
|
|
|
|
|
|
|
|
MsgType type = MsgType.values[msgType]; |
|
|
|
|
Uint8List data; |
|
|
|
|
if (type == MsgType.TEXT) { |
|
|
|
|
data = utf8.encode(content); |
|
|
|
|
} else if (type == MsgType.IMAGE || type == MsgType.VIDEO || type == MsgType.AUDIO) { |
|
|
|
|
File file = File(attach); |
|
|
|
|
Directory dir = await getTemporaryDirectory(); |
|
|
|
|
File newFile = await file.copy("${dir.path}/hx_${DateTime.now().millisecondsSinceEpoch}.${file.path.split(".")[1]}"); |
|
|
|
|
data = await file.readAsBytes(); |
|
|
|
|
attach = newFile.path; |
|
|
|
|
} else { |
|
|
|
|
data = utf8.encode(content); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Map<String, dynamic> message = createMessage(toId, content, fromId: userId, attach: attach, msgType: msgType, replyId: replyId); |
|
|
|
|
message["state"] = 1; |
|
|
|
|
int id = await hxDatabase.insert(message).catchError((error) { |
|
|
|
|
debugPrint("insertMessage: $error"); |
|
|
|
@ -186,8 +238,8 @@ class SocketClient {
|
|
|
|
|
return Message.fromJson(message); |
|
|
|
|
} |
|
|
|
|
message["id"] = id; |
|
|
|
|
Uint8List data = utf8.encode(content); |
|
|
|
|
MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.TEXT, data: data); |
|
|
|
|
|
|
|
|
|
MsgData msgData = MsgData(to: toId, from: userId, type: type, data: data); |
|
|
|
|
final proto2 = Proto(5, 1, msgData.writeToBuffer()); |
|
|
|
|
try { |
|
|
|
|
_socket.add(proto2.toBytes()); |
|
|
|
@ -202,6 +254,8 @@ class SocketClient {
|
|
|
|
|
return Message.fromJson(message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
checkSocket() { |
|
|
|
|
if (_socket == null) { |
|
|
|
|
reconnect(); |
|
|
|
|