zxl
10 小时以前 172933f098017bc4c4f57dcda0d490ea12bb13bb
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { Notification, MessageBox  } from "element-ui";
let wsSignIn; // ws 是否是掉线状态
import store from "@/store";
class WsSocket {
  /**
   * Websocket 连接
   *
   * @var Websocket
   */
  connect;
 
  /**
   * 服务器连接地址
   */
  url;
 
  /**
   * 配置信息
   *
   * @var Object
   */
  config = {
    heartbeat: {
      enabled: false, // 是否发送心跳包
      time: 10000, // 心跳包发送间隔时长
      setInterval: null, // 心跳包计时器
    },
    reconnect: {
      lockReconnect: false,
      setTimeout: null, // 计时器对象
      time: 1000, // 重连间隔时间
      number: 1000, // 重连次数
    },
  };
 
  /**
   * 自定义绑定消息事件
   *
   * @var Array
   */
  onCallBacks = [];
 
  /**
   * 创建 WsSocket 的实例
   *
   * @param {Function} urlCallBack url闭包函数
   * @param {Object} events 原生 WebSocket 绑定事件
   */
  constructor(urlCallBack, events) {
    this.urlCallBack = urlCallBack;
 
    // 定义 WebSocket 原生方法
    this.events = Object.assign(
      {
        onError: (evt) => {},
        onOpen: (evt) => {},
        onClose: (evt) => {},
      },
      events
    );
  }
 
  /**
   * 事件绑定
   *
   * @param {String} event 事件名
   * @param {Function} callBack 回调方法
   */
  on(event, callBack) {
    // 对应 socket-instance.js
    this.onCallBacks[event] = callBack;
    return this;
  }
 
  /**
   * 加载 WebSocket
   */
  loadSocket() {
    /**
     * 判断当前如果是掉线提示
     * 重连成功后关闭掉线提示,新增重连提示
     */
    if (wsSignIn) {
      store.commit('SET_WS_STATUS',true);
      wsSignIn.close();
 
      Notification({
        title: "成功",
        message: "重连成功",
        type: "success",
        position: "top-right",
        duration: 1000,
      });
    }
    // 判断当前是否已经连接
    if (this.connect != null) {
      this.connect.close();
      this.connect = null;
    }
 
    this.url = this.urlCallBack();
    const connect = new WebSocket(this.url);
    connect.onerror = this.onError.bind(this);
    connect.onopen = this.onOpen.bind(this);
    connect.onmessage = this.onMessage.bind(this);
    connect.onclose = this.onClose.bind(this);
    this.connect = connect;
    
    store.commit('SET_WS_STATUS',false);
  }
 
  /**
   * 连接 Websocket
   */
  connection() {
    this.loadSocket();
  }
 
  /**
   * 掉线重连 Websocket
   */
  reconnect() {
    
    /**
     * 长时间挂载页面中并且重连次数为空的时候进行提示
     */
    if (this.config.reconnect.number == 0) {
      MessageBox("当前对话链接已失效,请从关闭重新进入。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnPressEscape: false,
        closeOnClickModal: false,
        type: "warning",
      })
        .then(() => {
          window.close();
          Notification({
            title: "对话链接已失效提示",
            message: "请手动关闭当前页面",
            type: "error",
            position: "top-right",
          });
        })
        .catch(() => {
          Notification({
            title: "对话链接已失效提示",
            message: "请手动关闭当前页面",
            type: "error",
            position: "top-right",
          });
        });
      return false;
    }
    // 掉线重连提示
    wsSignIn = Notification({
      title: "掉线重连接提示",
      message: `网络连接已断开,正在尝试重新连接......`,
      type: "error",
      position: "top-right",
      duration: 0,
    });
    // 掉线更改消息状态
    store.commit('SET_WS_STATUS',true);
    let reconnect = this.config.reconnect;
    if (reconnect.lockReconnect || reconnect.number == 0) {
      return;
    }
    this.config.reconnect.lockReconnect = true;
    // 没连接上会一直重连,设置延迟避免请求过多
    reconnect.setTimeout && clearTimeout(reconnect.setTimeout);
    this.config.reconnect.setTimeout = setTimeout(() => {
      this.connection();
      this.config.reconnect.lockReconnect = false;
      this.config.reconnect.number--;
    }, reconnect.time);
  }
 
  /**
   * 解析接受的消息
   *
   * @param {Object} evt Websocket 消息
   */
  onParse(evt) {
    const res = JSON.parse(evt.data).result;
 
    //如果创建时间是时间戳类型则转换为 日期类型,否则新压入栈的消息的创建时间和从数据库读取出来的创建时间格式对不上,处理的时候会出异常。
    if (typeof res.createTime == "number") {
      res.createTime = this.unixToDate(res.createTime, "yyyy-MM-dd hh:mm");
    }
    return res;
  }
 
  /**
   * 将unix时间戳转换为指定格式
   * @param unix   时间戳【秒】
   * @param format 转换格式
   * @returns {*|string}
   */
  unixToDate(unix, format) {
    if (!unix) return unix;
    let _format = format || "yyyy-MM-dd hh:mm:ss";
    const d = new Date(unix);
    const o = {
      "M+": d.getMonth() + 1,
      "d+": d.getDate(),
      "h+": d.getHours(),
      "m+": d.getMinutes(),
      "s+": d.getSeconds(),
      "q+": Math.floor((d.getMonth() + 3) / 3),
      S: d.getMilliseconds(),
    };
    if (/(y+)/.test(_format))
      _format = _format.replace(
        RegExp.$1,
        (d.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    for (const k in o)
      if (new RegExp("(" + k + ")").test(_format))
        _format = _format.replace(
          RegExp.$1,
          RegExp.$1.length === 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
    return _format;
  }
 
  /**
   * 打开连接
   *
   * @param {Object} evt Websocket 消息
   */
  onOpen(evt) {
    this.events.onOpen(evt);
 
    if (this.config.heartbeat.enabled) {
      this.heartbeat();
    }
  }
 
  /**
   * 关闭连接
   *
   * @param {Object} evt Websocket 消息
   */
  onClose(evt) {
    console.log("关闭连接", evt);
    if (this.config.heartbeat.enabled) {
      clearInterval(this.config.heartbeat.setInterval);
    }
    console.log("evt", evt);
 
    if (evt.code == 1006) {
      this.reconnect();
    }
 
    // this.events.onClose(evt);
  }
 
  /**
   * 连接错误
   *
   * @param {Object} evt Websocket 消息
   */
  onError(evt) {
    this.events.onError(evt);
  }
 
  /**
   * 接收消息
   *
   * @param {Object} evt Websocket 消息
   */
  onMessage(evt) {
    let result = this.onParse(evt);
    if (
      this.onParse(evt).text.includes("goodsName") ||
      this.onParse(evt).text.includes("groupName")
    ) {
      let params = {
        ...this.onParse(evt),
        text: JSON.parse(this.onParse(evt).text),
      };
      this.onCallBacks["event_talk"](params);
    } else {
      let params = {
        ...this.onParse(evt),
        text: this.onParse(evt).text,
      };
      this.onCallBacks["event_talk"](params);
    }
    // 指定推送消息
  }
  /**
   * WebSocket心跳检测
   */
  heartbeat() {
    console.log("WebSocket心跳检测");
    this.config.heartbeat.setInterval = setInterval(() => {
      this.connect.send("PING");
    }, this.config.heartbeat.time);
  }
 
  /**
   * 聊天发送数据
   *
   * @param {Object} message
   */
  send(message) {
    this.connect.send(JSON.stringify(message));
  }
 
  /**
   * 关闭连接
   */
  close() {
    this.connect.close();
  }
 
  /**
   * 推送消息
   *
   * @param {String} event 事件名
   * @param {Object} data 数据
   */
  emit(event, data) {
    if (this.connect && this.connect.readyState === 1) {
      this.connect.send(JSON.stringify(data));
    } else {
      console.error("WebSocket 连接已关闭...", this.connect);
    }
  }
}
 
export default WsSocket;