websocket.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * WebSocket状态管理模块
  3. *
  4. * @author kxmall
  5. * @date 2025-01-08
  6. */
  7. import riderWebSocketManager from '@/utils/websocket.js';
  8. const state = {
  9. // 连接状态
  10. connectionStatus: 'disconnected', // disconnected, connecting, connected, error
  11. // 在线状态
  12. isOnline: false,
  13. // 最后连接时间
  14. lastConnectedTime: null,
  15. // 最后断开时间
  16. lastDisconnectedTime: null,
  17. // 重连次数
  18. reconnectAttempts: 0,
  19. // 接收到的消息数量
  20. receivedMessageCount: 0,
  21. // 最后接收消息时间
  22. lastMessageTime: null,
  23. // WebSocket配置
  24. config: {
  25. enabled: true,
  26. autoReconnect: true,
  27. enableNotifications: true
  28. }
  29. };
  30. const mutations = {
  31. // 设置连接状态
  32. SET_CONNECTION_STATUS(state, status) {
  33. state.connectionStatus = status;
  34. state.isOnline = status === 'connected';
  35. if (status === 'connected') {
  36. state.lastConnectedTime = new Date();
  37. state.reconnectAttempts = 0;
  38. } else if (status === 'disconnected') {
  39. state.lastDisconnectedTime = new Date();
  40. }
  41. },
  42. // 增加重连次数
  43. INCREMENT_RECONNECT_ATTEMPTS(state) {
  44. state.reconnectAttempts++;
  45. },
  46. // 重置重连次数
  47. RESET_RECONNECT_ATTEMPTS(state) {
  48. state.reconnectAttempts = 0;
  49. },
  50. // 增加接收消息数量
  51. INCREMENT_MESSAGE_COUNT(state) {
  52. state.receivedMessageCount++;
  53. state.lastMessageTime = new Date();
  54. },
  55. // 更新WebSocket配置
  56. UPDATE_CONFIG(state, config) {
  57. state.config = { ...state.config, ...config };
  58. },
  59. // 重置状态
  60. RESET_STATE(state) {
  61. state.connectionStatus = 'disconnected';
  62. state.isOnline = false;
  63. state.reconnectAttempts = 0;
  64. state.receivedMessageCount = 0;
  65. state.lastConnectedTime = null;
  66. state.lastDisconnectedTime = null;
  67. state.lastMessageTime = null;
  68. }
  69. };
  70. const actions = {
  71. // 连接WebSocket
  72. connect({ commit, rootState }) {
  73. if (!rootState.userInfo || !rootState.userInfo.state) {
  74. console.log('用户未通过审核,无法连接WebSocket');
  75. return false;
  76. }
  77. commit('SET_CONNECTION_STATUS', 'connecting');
  78. try {
  79. riderWebSocketManager.connect(rootState.userInfo, rootState.baseUrl);
  80. return true;
  81. } catch (error) {
  82. console.error('连接WebSocket失败:', error);
  83. commit('SET_CONNECTION_STATUS', 'error');
  84. return false;
  85. }
  86. },
  87. // 断开WebSocket
  88. disconnect({ commit }) {
  89. riderWebSocketManager.disconnect();
  90. commit('SET_CONNECTION_STATUS', 'disconnected');
  91. },
  92. // 重连WebSocket
  93. reconnect({ dispatch, commit }) {
  94. commit('INCREMENT_RECONNECT_ATTEMPTS');
  95. return dispatch('connect');
  96. },
  97. // 发送消息
  98. sendMessage({ state }, message) {
  99. if (state.connectionStatus !== 'connected') {
  100. console.warn('WebSocket未连接,无法发送消息');
  101. return false;
  102. }
  103. return riderWebSocketManager.send(message);
  104. },
  105. // 更新配置
  106. updateConfig({ commit }, config) {
  107. commit('UPDATE_CONFIG', config);
  108. },
  109. // 处理连接状态变化
  110. handleStatusChange({ commit }, status) {
  111. commit('SET_CONNECTION_STATUS', status);
  112. },
  113. // 处理接收到消息
  114. handleMessage({ commit }, message) {
  115. commit('INCREMENT_MESSAGE_COUNT');
  116. },
  117. // 重置所有状态
  118. reset({ commit }) {
  119. commit('RESET_STATE');
  120. }
  121. };
  122. const getters = {
  123. // 是否已连接
  124. isConnected: state => state.connectionStatus === 'connected',
  125. // 是否正在连接
  126. isConnecting: state => state.connectionStatus === 'connecting',
  127. // 是否有错误
  128. hasError: state => state.connectionStatus === 'error',
  129. // 连接状态文本
  130. statusText: state => {
  131. switch (state.connectionStatus) {
  132. case 'connected': return '已连接';
  133. case 'connecting': return '连接中';
  134. case 'disconnected': return '已断开';
  135. case 'error': return '连接错误';
  136. default: return '未知状态';
  137. }
  138. },
  139. // 连接时长(毫秒)
  140. connectionDuration: state => {
  141. if (state.connectionStatus === 'connected' && state.lastConnectedTime) {
  142. return Date.now() - state.lastConnectedTime.getTime();
  143. }
  144. return 0;
  145. },
  146. // 是否需要重连
  147. shouldReconnect: state => {
  148. return state.config.autoReconnect &&
  149. state.connectionStatus === 'disconnected' &&
  150. state.reconnectAttempts < 5;
  151. }
  152. };
  153. export default {
  154. namespaced: true,
  155. state,
  156. mutations,
  157. actions,
  158. getters
  159. };