liming vor 1 Woche
Ursprung
Commit
0ae1a4a79f
1 geänderte Dateien mit 19 neuen und 21 gelöschten Zeilen
  1. 19 21
      src/stores/modules/webSocketStore.js

+ 19 - 21
src/stores/modules/webSocketStore.js

@@ -3,7 +3,7 @@ import { defineStore } from "pinia";
 import { $root as protobuf } from "@/common/proto/proto";
 import * as Constant from "@/common/constant/Constant";
 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";
 
 export const useWebSocketStore = defineStore("webSocketStore", {
@@ -17,7 +17,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
     reconnectInterval: 3000, // 重连间隔
     messages: [], // 消息列表
     unreadMessages: [], // 未读消息列表
-    uuid: null, 
+    uuid: null,
 
     // 心跳检测配置
     heartCheck: {
@@ -32,19 +32,17 @@ export const useWebSocketStore = defineStore("webSocketStore", {
   getters: {
     isConnected: (state) => state.socket?.readyState === WebSocket.OPEN,
     hasUnreadMessages: (state) => state.unreadMessages.length > 0,
-
-   
   },
 
   // 方法
   actions: {
-     async getMessages(params){
+    async getMessages(params) {
       const { data } = await getMessageApi({
         messageType: params.messageType,
         uuid: params.uuid,
-        friendUsername: params.friendUsername, 
-      })
-      this.messages = data || []
+        friendUsername: params.friendUsername,
+      });
+      this.messages = data || [];
     },
     // 初始化socket
     startHeartbeat() {
@@ -85,7 +83,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
     },
     // 链接ws
     connect(userUuid) {
-      if(!userUuid) return
+      if (!userUuid) return;
       console.log("开始连接...");
       this.disconnect(); // 确保先断开现有连接
 
@@ -134,12 +132,12 @@ export const useWebSocketStore = defineStore("webSocketStore", {
           });
 
           console.log("收到消息:", message);
-          if(MSG_TYPE_MAP.includes(message.messageType)){
+          if (MSG_TYPE_MAP.includes(message.messageType)) {
             // 更新状态
             this.messages.push({
               ...message,
               toUsername: message.to,
-            }); 
+            });
           }
 
           if (message.type === "heatbeat") return;
@@ -157,7 +155,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
 
       reader.readAsArrayBuffer(data);
     },
-    // 处理WebRTC消息    
+    // 处理WebRTC消息
     webrtcConnection() {
       if (!this.peer) return;
 
@@ -185,7 +183,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
         }
       };
     },
-    // 断线重连    
+    // 断线重连
     reconnect() {
       if (
         this.lockConnection ||
@@ -219,7 +217,7 @@ export const useWebSocketStore = defineStore("webSocketStore", {
       }
       this.reconnectAttempts = 0;
     },
-    // 发送消息    
+    // 发送消息
     sendMessage(messageData) {
       if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
         console.error("WebSocket未连接");
@@ -232,19 +230,19 @@ export const useWebSocketStore = defineStore("webSocketStore", {
         fromUsername: walletStore.username,
         from: walletStore.account,
       };
-      if(MSG_TYPE_MAP.includes(data.messageType)){
-        this.messages.push({
-          ...data,
-          toUsername: data.friendUsername
-        });  
-      }
+
       try {
         const MessageType = protobuf.lookupType("protocol.Message");
         const messagePB = MessageType.create(data);
         const buffer = MessageType.encode(messagePB).finish();
         this.socket.send(buffer);
+        if (MSG_TYPE_MAP.includes(data.messageType)) {
+          this.messages.push({
+            ...data,
+            toUsername: data.friendUsername,
+          });
+        }
 
-        
         return true;
       } catch (error) {
         console.error("消息编码错误:", error);