浏览代码

1、websocket调整不存在的情况下

yys 1 月之前
父节点
当前提交
9204dab63f
共有 4 个文件被更改,包括 30 次插入12 次删除
  1. 4 4
      src/store/index.js
  2. 8 2
      src/utils/liveWS.js
  3. 9 3
      src/views/live/liveConfig/index.vue
  4. 9 3
      src/views/live/liveConsole/LiveConsole.vue

+ 4 - 4
src/store/index.js

@@ -34,15 +34,15 @@ const store = new Vuex.Store({
     }
   },
   actions: {
-    // 修改 action 以正确传递参数
-    initLiveWs({ commit, state }, { liveWsUrl, liveId, userId }) {
+    // 修改 action 以正确传递参数(含多租户 tenantCode)
+    initLiveWs({ commit, state }, { liveWsUrl, liveId, userId, tenantCode }) {
       // 如果已经存在对应 liveId 的连接,先关闭它
       if (state.liveWs[liveId]) {
         state.liveWs[liveId].close();
       }
 
-      // 创建新的 WebSocket 连接
-      const ws = new LiveWS(liveWsUrl, liveId, userId);
+      // 创建新的 WebSocket 连接(传入 tenantCode 进行多租户隔离)
+      const ws = new LiveWS(liveWsUrl, liveId, userId, tenantCode || undefined);
 
       // 提交到 mutation
       commit('setLiveWs', { ws, liveId });

+ 8 - 2
src/utils/liveWS.js

@@ -5,19 +5,25 @@ export class LiveWS {
    * @param {string} url - WebSocket 服务器地址
    * @param {number} liveId - 直播间ID
    * @param {number} userId - 用户ID
+   * @param {string} tenantCode - 租户编码(多租户隔离)
    * @param {number} checkInterval - 检查连接状态的时间间隔,单位毫秒
    * @param {number} reconnectDelay - 连接断开后重连的延迟,单位毫秒
    */
-  constructor(url, liveId, userId, checkInterval = 5000, reconnectDelay = 3000) {
+  constructor(url, liveId, userId, tenantCode, checkInterval = 5000, reconnectDelay = 3000) {
     let timestamp = new Date().getTime()
     let userType = 1
     let signature = CryptoJS.HmacSHA256(
       CryptoJS.enc.Utf8.parse('' + liveId + userId + userType + timestamp),
       CryptoJS.enc.Utf8.parse(timestamp)).toString(CryptoJS.enc.Hex)
-    this.url = url + `?liveId=${liveId}&userId=${userId}&userType=${userType}&timestamp=${timestamp}&signature=${signature}`;
+    let query = `liveId=${liveId}&userId=${userId}&userType=${userType}&timestamp=${timestamp}&signature=${signature}`;
+    if (tenantCode && tenantCode !== 'undefined' && tenantCode !== 'null') {
+      query += `&tenantCode=${tenantCode}`;
+    }
+    this.url = url + '?' + query;
     console.log(this.url)
     this.liveId = liveId;
     this.userId = userId;
+    this.tenantCode = tenantCode;
     this.checkInterval = checkInterval;
     this.reconnectDelay = reconnectDelay;
     this.ws = null;

+ 9 - 3
src/views/live/liveConfig/index.vue

@@ -72,6 +72,7 @@ import LiveRedConf from './liveRedConf.vue'
 import LiveLotteryConf from './liveLotteryConf.vue'
 import LiveReplay from './liveReplay.vue'
 import Preview from './preview.vue'
+import Cookies from 'js-cookie'
 import LiveCoupon from './liveCoupon.vue'
 import Barrage from './barrage.vue'
 import { listLive, getLive, delLive, addLive, updateLive, exportLive,selectCompanyTalent,handleShelfOrUn,handleDeleteSelected } from "@/api/live/live";
@@ -129,11 +130,16 @@ export default {
   },
   methods: {
     connectWebSocket() {
-      this.$store.dispatch('initLiveWs', {
+      const params = {
         liveWsUrl: this.liveWsUrl,
         liveId: this.liveId,
-        userId: this.userId
-      })
+        userId: this.userId,
+      };
+      const tenantCode = Cookies.get('tenantCode');
+      if (tenantCode) {
+        params.tenantCode = tenantCode;
+      }
+      this.$store.dispatch('initLiveWs', params)
       this.socket = this.$store.state.liveWs[this.liveId]
       this.socket.onmessage = (event) => this.handleWsMessage(event)
     },

+ 9 - 3
src/views/live/liveConsole/LiveConsole.vue

@@ -267,6 +267,7 @@ import { listLiveSingleMsg,delLiveMsg } from '@/api/live/liveMsg'
 import { getLive } from '@/api/live/live'
 import { consoleList } from '@/api/live/task'
 import ScreenScale from './ScreenScale.vue'; // 路径根据实际位置调整
+import Cookies from 'js-cookie'
 
 
 export default {
@@ -832,11 +833,16 @@ export default {
       })
     },
     connectWebSocket() {
-      this.$store.dispatch('initLiveWs', {
+      const params = {
         liveWsUrl: this.liveWsUrl,
         liveId: this.liveId,
-        userId: this.userId
-      })
+        userId: this.userId,
+      };
+      const tenantCode = Cookies.get('tenantCode');
+      if (tenantCode) {
+        params.tenantCode = tenantCode;
+      }
+      this.$store.dispatch('initLiveWs', params)
       this.socket = this.$store.state.liveWs[this.liveId]
       this.socket.onmessage = (event) => this.handleWsMessage(event)
     },