|
@@ -6,6 +6,7 @@ import { useWalletStore } from "@/stores/modules/walletStore";
|
|
|
import { getMessageApi } from "@/api/path/im.api";
|
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
|
|
|
|
+
|
|
|
import {
|
|
|
setMessageHook,
|
|
|
handleMessageHook,
|
|
@@ -52,6 +53,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
|
|
|
// 方法
|
|
|
actions: {
|
|
|
+ // 获取消息
|
|
|
async getMessages(params) {
|
|
|
const { data } = await getMessageApi({
|
|
|
messageType: params.messageType,
|
|
@@ -64,6 +66,34 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
return item;
|
|
|
}) || [];
|
|
|
},
|
|
|
+ // 发送消息
|
|
|
+ sendMessage(messageData) {
|
|
|
+ if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
|
+ console.error("WebSocket未连接");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取url上uuid参数
|
|
|
+ const walletStore = useWalletStore();
|
|
|
+ let data = {
|
|
|
+ ...messageData,
|
|
|
+ fromUsername: walletStore.username,
|
|
|
+ from: walletStore.account,
|
|
|
+ to: this.toUserInfo.uuid,
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ const MessageType = protobuf.lookupType("protocol.Message");
|
|
|
+ const messagePB = MessageType.create(data);
|
|
|
+ const buffer = MessageType.encode(messagePB).finish();
|
|
|
+ this.socket.send(buffer);
|
|
|
+ data.avatar = walletStore.avatar;
|
|
|
+ setMessageHook(data, this);
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.error("消息编码错误:", error);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ },
|
|
|
// 初始化socket
|
|
|
startHeartbeat() {
|
|
|
const self = this;
|
|
@@ -135,33 +165,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
this.reconnect();
|
|
|
};
|
|
|
},
|
|
|
- // 发送消息
|
|
|
- sendMessage(messageData) {
|
|
|
- if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
|
|
|
- console.error("WebSocket未连接");
|
|
|
- return false;
|
|
|
- }
|
|
|
|
|
|
- const walletStore = useWalletStore();
|
|
|
- let data = {
|
|
|
- ...messageData,
|
|
|
- fromUsername: walletStore.username,
|
|
|
- from: walletStore.account,
|
|
|
- };
|
|
|
- console.log("发送消息=", data);
|
|
|
- try {
|
|
|
- const MessageType = protobuf.lookupType("protocol.Message");
|
|
|
- const messagePB = MessageType.create(data);
|
|
|
- const buffer = MessageType.encode(messagePB).finish();
|
|
|
- this.socket.send(buffer);
|
|
|
- data.avatar = walletStore.avatar;
|
|
|
- setMessageHook(data, this);
|
|
|
- return true;
|
|
|
- } catch (error) {
|
|
|
- console.error("消息编码错误:", error);
|
|
|
- return false;
|
|
|
- }
|
|
|
- },
|
|
|
// 处理消息
|
|
|
handleMessage(data) {
|
|
|
const MessageType = protobuf.lookupType("protocol.Message");
|