ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable no-console */
/* eslint-disable unicorn/prefer-add-event-listener */
import { ref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
 
import { useAppConfig } from '@vben/hooks';
import { useAccessStore, useUserStore } from '@vben/stores';
 
import ReconnectingWebSocket from 'reconnecting-websocket';
 
import { $t } from '#/locales';
import { useAuthStore } from '#/store';
import { getRealJnpfAppEnCode } from '#/utils/jnpf';
 
const userStore = useUserStore();
const userInfo = userStore.getUserInfo;
const { createMessage, createWarningModal } = useMessage();
const { isDevMode, webSocketURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
 
const url = isDevMode() ? `${webSocketURL}/api/message/websocket/` : webSocketURL ? `${webSocketURL}/websocket/` : `${window.location.origin}/websocket/`;
const webSocketUrl = url.replace('https://', 'wss://').replace('http://', 'ws://');
 
let ws: any;
const listeners = new Map();
 
export function useWebSocket() {
  const accessStore = useAccessStore();
  const authStore = useAuthStore();
  const token = accessStore.accessToken;
  const server = ref(webSocketUrl + encodeURIComponent(token as string));
  /** 初始化WebSocket */
  function initWebSocket() {
    if (ws) {
      ws.close();
      ws = null;
    }
    ws = new ReconnectingWebSocket(server.value);
 
    ws.addEventListener('open', onOpen);
    ws.onerror = onError;
    ws.onmessage = onMessage;
 
    function onOpen() {
      const onConnection = { method: 'OnConnection', mobileDevice: false, systemId: userInfo?.systemId, isSeparate: !!getRealJnpfAppEnCode() };
      sendWsMsg(JSON.stringify(onConnection));
    }
 
    function onError(e) {
      console.log('[WebSocket] 连接发生错误:', e);
    }
 
    function onMessage(res) {
      if (res.data) {
        try {
          const dataStr = res.data;
          const data = JSON.parse(dataStr);
          for (const callback of listeners.keys()) {
            try {
              callback(data);
            } catch (error) {
              console.error(error);
            }
          }
          // initMessage: //初始化
          // sendMessage: //发送消息
          // receiveMessage: //接收消息
          // messageList: //消息列表
          // messagePush: //消息推送
          switch (data.method) {
            // 断开websocket连接
            case 'closeSocket': {
              if (ws) {
                ws.close();
                ws = null;
              }
              break;
            }
            // 用户过期
            case 'logout': {
              if (data.token && data.token !== token) return location.reload();
              if (ws) {
                ws.close();
                ws = null;
              }
              createMessage.error(data.msg || '登录过期,请重新登录').then(() => {
                accessStore.setAccessToken(null);
                authStore.logout();
              });
              break;
            }
            // 刷新页面
            case 'refresh': {
              createWarningModal({
                title: $t('common.tipTitle'),
                content: data.msg || '您的权限发生变更,请刷新界面!',
                okText: '刷新',
                onOk: () => {
                  location.reload();
                },
              });
              break;
            }
            default: {
              break;
            }
          }
        } catch (error) {
          console.error('[WebSocket] data解析失败:', error);
        }
      }
    }
  }
  /**
   * 添加 WebSocket 消息监听
   * @param callback
   */
  function onWebSocket(callback: (data: object) => any) {
    if (!listeners.has(callback)) {
      if (typeof callback === 'function') {
        listeners.set(callback, null);
      } else {
        console.debug('[WebSocket] 添加 WebSocket 消息监听失败:传入的参数不是一个方法');
      }
    }
  }
 
  /**
   * 解除 WebSocket 消息监听
   *
   * @param callback
   */
  function offWebSocket(callback: (data: object) => any) {
    listeners.delete(callback);
  }
 
  function getWebSocket() {
    return ws;
  }
 
  function sendWsMsg(msg: string) {
    try {
      const msgObj = JSON.parse(msg);
      msgObj.token = token;
      const content = JSON.stringify(msgObj);
      ws.send(content);
    } catch {}
  }
 
  function closeWebSocket() {
    if (ws) {
      ws.close();
      ws = null;
    }
  }
 
  return {
    initWebSocket,
    sendWsMsg,
    onWebSocket,
    offWebSocket,
    getWebSocket,
    closeWebSocket,
  };
}