index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import app from './modules/app'
  4. import user from './modules/user'
  5. import tagsView from './modules/tagsView'
  6. import permission from './modules/permission'
  7. import settings from './modules/settings'
  8. import getters from './getters'
  9. import {LiveWS} from "@/utils/liveWS";
  10. Vue.use(Vuex)
  11. const store = new Vuex.Store({
  12. modules: {
  13. app,
  14. user,
  15. tagsView,
  16. permission,
  17. settings
  18. },
  19. getters,
  20. state: {
  21. liveWs: {}
  22. },
  23. mutations: {
  24. // 更新为支持多个 WebSocket 连接
  25. setLiveWs(state, { ws, liveId }) {
  26. // 使用 liveId 作为键来存储不同的 WebSocket 连接
  27. Vue.set(state.liveWs, liveId, ws);
  28. },
  29. // 添加移除 WebSocket 连接的 mutation
  30. removeLiveWs(state, liveId) {
  31. Vue.delete(state.liveWs, liveId);
  32. }
  33. },
  34. actions: {
  35. // 修改 action 以正确传递参数
  36. initLiveWs({ commit, state }, { liveWsUrl, liveId, userId }) {
  37. // 如果已经存在对应 liveId 的连接,先关闭它
  38. if (state.liveWs[liveId]) {
  39. state.liveWs[liveId].close();
  40. }
  41. // 创建新的 WebSocket 连接
  42. const ws = new LiveWS(liveWsUrl, liveId, userId);
  43. // 提交到 mutation
  44. commit('setLiveWs', { ws, liveId });
  45. return ws;
  46. },
  47. // 添加关闭特定 WebSocket 连接的 action
  48. closeLiveWs({ commit, state }, liveId) {
  49. if (state.liveWs[liveId]) {
  50. state.liveWs[liveId].close();
  51. commit('removeLiveWs', liveId);
  52. }
  53. }
  54. }
  55. })
  56. export default store