Przeglądaj źródła

直播间消息的展示及中控台设置用户自见等功能存在bug无法正常使用
销售公司去掉新增直播功能按钮

yys 5 dni temu
rodzic
commit
07af2f0154
2 zmienionych plików z 23 dodań i 17 usunięć
  1. 9 7
      src/store/index.js
  2. 14 10
      src/views/live/liveConsole/LiveConsole.vue

+ 9 - 7
src/store/index.js

@@ -38,24 +38,26 @@ const store = new Vuex.Store({
   actions: {
     // 修改 action 以正确传递参数
     initLiveWs({ commit, state }, { liveWsUrl, liveId, userId }) {
+      const normalizedLiveId = String(liveId);
       // 如果已经存在对应 liveId 的连接,先关闭它
-      if (state.liveWs[liveId]) {
-        state.liveWs[liveId].close();
+      if (state.liveWs[normalizedLiveId]) {
+        state.liveWs[normalizedLiveId].close();
       }
 
       // 创建新的 WebSocket 连接
-      const ws = new LiveWS(liveWsUrl, liveId, userId);
+      const ws = new LiveWS(liveWsUrl, normalizedLiveId, userId);
 
       // 提交到 mutation
-      commit('setLiveWs', { ws, liveId });
+      commit('setLiveWs', { ws, liveId: normalizedLiveId });
 
       return ws;
     },
     // 添加关闭特定 WebSocket 连接的 action
     closeLiveWs({ commit, state }, liveId) {
-      if (state.liveWs[liveId]) {
-        state.liveWs[liveId].close();
-        commit('removeLiveWs', liveId);
+      const normalizedLiveId = String(liveId);
+      if (state.liveWs[normalizedLiveId]) {
+        state.liveWs[normalizedLiveId].close();
+        commit('removeLiveWs', normalizedLiveId);
       }
     }
   }

+ 14 - 10
src/views/live/liveConsole/LiveConsole.vue

@@ -454,7 +454,7 @@ export default {
       return `msg-${index}-${m.userId || 'unknown'}`;
     },
     ensureSocket() {
-      if (!this.socket || !this.socket.ws || this.socket.ws.readyState !== WebSocket.OPEN) {
+      if (!this.socket) {
         this.$message.error('WebSocket 未连接,请稍后重试');
         return false;
       }
@@ -718,8 +718,9 @@ export default {
       this.newMsg = '';
     },
     handleWsMessage(event) {
-      let { code, data } = JSON.parse(event.data)
-      if (code === 200) {
+      try {
+        let { code, data } = JSON.parse(event.data)
+        if (code === 200) {
         let { cmd } = data
         if (cmd === 'sendMsg') {
           let message = JSON.parse(data.data)
@@ -814,6 +815,9 @@ export default {
         } else if (cmd === 'globalVisible') {
           this.globalVisible = Number(data.status) === 1 || data.status === true;
         }
+        }
+      } catch (e) {
+        console.error('WebSocket message parse error:', e);
       }
     },
     getLive(){
@@ -859,16 +863,16 @@ export default {
       })
     },
     connectWebSocket() {
+      const liveId = String(this.liveId);
       this.$store.dispatch('initLiveWs', {
         liveWsUrl: this.liveWsUrl,
-        liveId: this.liveId,
+        liveId,
         userId: this.userId
-      }).then((ws) => {
-        this.socket = ws || this.$store.state.liveWs[this.liveId]
-        if (this.socket) {
-          this.socket.onmessage = (event) => this.handleWsMessage(event)
-        }
-      })
+      });
+      this.socket = this.$store.state.liveWs[liveId];
+      if (this.socket) {
+        this.socket.onmessage = (event) => this.handleWsMessage(event);
+      }
     },
     changeUserState(u) {
       const displayList = this[`${this.currentTab}DisplayList`];