|
@@ -6,6 +6,11 @@ import { useWalletStore } from "@/stores/modules/walletStore";
|
|
import { getMessageApi } from "@/api/path/im.api";
|
|
import { getMessageApi } from "@/api/path/im.api";
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
import { MSG_TYPE, MSG_TYPE_MAP } from "@/common/constant/msgType";
|
|
|
|
|
|
|
|
+import {
|
|
|
|
+ setMessageHook,
|
|
|
|
+ handleMessageHook,
|
|
|
|
+} from "@/views/im/hook/messagesHook";
|
|
|
|
+
|
|
const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
|
|
const IM_PATH = import.meta.env.VITE_IM_PATH_FIlE;
|
|
|
|
|
|
export const useWebSocketStore = defineStore("webSocketStore", {
|
|
export const useWebSocketStore = defineStore("webSocketStore", {
|
|
@@ -130,6 +135,33 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
this.reconnect();
|
|
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) {
|
|
handleMessage(data) {
|
|
const MessageType = protobuf.lookupType("protocol.Message");
|
|
const MessageType = protobuf.lookupType("protocol.Message");
|
|
@@ -148,37 +180,13 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
|
|
|
|
console.log("收到消息:", message);
|
|
console.log("收到消息:", message);
|
|
// 处理消息
|
|
// 处理消息
|
|
- // if (MSG_TYPE_MAP.includes(message.messageType)) {
|
|
|
|
-
|
|
|
|
- // 文本消息
|
|
|
|
- message.avatar = this.toUserInfo.avatar;
|
|
|
|
- if (message.contentType === MSG_TYPE.TEXT) {
|
|
|
|
- this.messages.push({
|
|
|
|
- ...message,
|
|
|
|
- toUsername: message.to,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- // 音频消息
|
|
|
|
- if (message.contentType === MSG_TYPE.AUDIO) {
|
|
|
|
- const audioBlob = new Blob([message.file], {
|
|
|
|
- type: `audio/${message.fileSuffix}`,
|
|
|
|
- });
|
|
|
|
- // 生成可播放的 ObjectURL
|
|
|
|
- const audioUrl = URL.createObjectURL(audioBlob);
|
|
|
|
- this.messages.push({
|
|
|
|
- ...message,
|
|
|
|
- file: audioUrl,
|
|
|
|
- toUsername: message.to,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ handleMessageHook(message, this);
|
|
if (message.type === "heatbeat") return;
|
|
if (message.type === "heatbeat") return;
|
|
|
|
|
|
if (message.type === Constant.MESSAGE_TRANS_TYPE) {
|
|
if (message.type === Constant.MESSAGE_TRANS_TYPE) {
|
|
this.dealWebRtcMessage(message);
|
|
this.dealWebRtcMessage(message);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
-
|
|
|
|
// 其他消息处理逻辑...
|
|
// 其他消息处理逻辑...
|
|
} catch (error) {
|
|
} catch (error) {
|
|
console.error("消息解码错误:", error);
|
|
console.error("消息解码错误:", error);
|
|
@@ -249,53 +257,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
|
|
}
|
|
}
|
|
this.reconnectAttempts = 0;
|
|
this.reconnectAttempts = 0;
|
|
},
|
|
},
|
|
- // 发送消息
|
|
|
|
- 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;
|
|
|
|
- // 文本消息
|
|
|
|
- if (data.contentType == MSG_TYPE.TEXT) {
|
|
|
|
- this.messages.push({
|
|
|
|
- ...data,
|
|
|
|
- toUsername: data.friendUsername,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 音频消息
|
|
|
|
- if (data.contentType === MSG_TYPE.AUDIO) {
|
|
|
|
- const blob = new Blob([data.file], { type: data.fileSuffix });
|
|
|
|
- const url = URL.createObjectURL(blob);
|
|
|
|
- console.log("url=", url);
|
|
|
|
- this.messages.push({
|
|
|
|
- ...data,
|
|
|
|
- toUsername: data.to,
|
|
|
|
- localUrl: url,
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error("消息编码错误:", error);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
// 发送WebRTC消息
|
|
// 发送WebRTC消息
|
|
dealWebRtcMessage(message) {
|
|
dealWebRtcMessage(message) {
|
|
// 实现WebRTC消息处理逻辑
|
|
// 实现WebRTC消息处理逻辑
|