From 5f279c87cf7973dfc11a4fcc63521354756a5cc6 Mon Sep 17 00:00:00 2001 From: zsw Date: Fri, 27 Sep 2024 16:36:33 +0800 Subject: [PATCH] message type --- lib/im/SocketClient.dart | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/im/SocketClient.dart b/lib/im/SocketClient.dart index 1ea16362..c4d3821e 100644 --- a/lib/im/SocketClient.dart +++ b/lib/im/SocketClient.dart @@ -208,8 +208,8 @@ class SocketClient { } } - Future sendMessage(String toId, String content) async { - Map message = createMessage(toId, content, fromId: userId); + Future sendMessage(String toId, String content, {String attach, int msgType, replyId}) async { + Map 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"); @@ -223,8 +223,19 @@ 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); + + 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); + data = await file.readAsBytes(); + } else { + data = utf8.encode(content); + } + + MsgData msgData = MsgData(to: toId, from: userId, type: type, data: data); final proto2 = Proto(5, 1, msgData.writeToBuffer()); try { _socket.add(proto2.toBytes()); @@ -239,6 +250,8 @@ class SocketClient { return Message.fromJson(message); } + + checkSocket() { if (_socket == null) { reconnect();