system-clients 4.0.18 → 4.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -200,7 +200,8 @@ let createWaterMark = function (userName) {
|
|
|
200
200
|
gasbrandIdData: [],
|
|
201
201
|
showRemind: false,
|
|
202
202
|
dataRemind: [],
|
|
203
|
-
connectNumber:
|
|
203
|
+
connectNumber: 0,
|
|
204
|
+
maxReconnectCount: 3,
|
|
204
205
|
timeout: 1000,
|
|
205
206
|
timeoutObj: null,
|
|
206
207
|
serverTimeoutObj: null,
|
|
@@ -311,52 +312,117 @@ let createWaterMark = function (userName) {
|
|
|
311
312
|
//如果服务端在一定时间内没有收到信息,就认为客户端断开了,服务端就会断开连接
|
|
312
313
|
// 发送心跳--确保连接有效
|
|
313
314
|
start () {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this.
|
|
317
|
-
this
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
315
|
+
this.reset()
|
|
316
|
+
// 检查WebSocket连接状态
|
|
317
|
+
if (this.$socket && this.$socket.readyState === WebSocket.OPEN) {
|
|
318
|
+
this.timeoutObj = setTimeout(() => {
|
|
319
|
+
try {
|
|
320
|
+
if (this.$socket && this.$socket.readyState === WebSocket.OPEN) {
|
|
321
|
+
this.$socket.send('ping')
|
|
322
|
+
console.log('发送心跳: ping')
|
|
323
|
+
this.serverTimeoutObj = setTimeout(() => {
|
|
324
|
+
console.log('心跳超时,关闭连接')
|
|
325
|
+
if (this.$socket) {
|
|
326
|
+
this.$socket.close()
|
|
327
|
+
}
|
|
328
|
+
}, this.timeout)
|
|
329
|
+
}
|
|
330
|
+
} catch (e) {
|
|
331
|
+
console.error('发送心跳失败:', e)
|
|
332
|
+
this.reconnect()
|
|
333
|
+
}
|
|
321
334
|
}, this.timeout)
|
|
322
|
-
}
|
|
335
|
+
}
|
|
323
336
|
},
|
|
324
337
|
// 重连
|
|
325
338
|
reconnect () {
|
|
326
|
-
|
|
339
|
+
console.log('reconnect()被调用,当前状态:', {
|
|
340
|
+
lockReconnect: this.lockReconnect,
|
|
341
|
+
connectNumber: this.connectNumber,
|
|
342
|
+
maxReconnectCount: this.maxReconnectCount
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
if (this.lockReconnect) {
|
|
346
|
+
console.log('重连被锁定,跳过')
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 检查重连次数
|
|
351
|
+
this.connectNumber++
|
|
352
|
+
if (this.connectNumber > this.maxReconnectCount) {
|
|
353
|
+
console.log(`重连失败,已达到最大重连次数(${this.maxReconnectCount}),停止重连`)
|
|
354
|
+
this.$showMessage(`WebSocket连接失败,已尝试${this.maxReconnectCount}次重连,请刷新页面或联系管理员`)
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
this.lockReconnect = true
|
|
359
|
+
|
|
360
|
+
// 清理旧连接
|
|
361
|
+
if (this.$socket) {
|
|
362
|
+
this.$socket.close()
|
|
363
|
+
this.$socket = null
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// 指数退避延迟
|
|
367
|
+
const delay = Math.min(1000 * Math.pow(2, this.connectNumber - 1), 30000)
|
|
368
|
+
console.log(`连接异常,${delay/1000}秒后进行第${this.connectNumber}次重连...`)
|
|
369
|
+
|
|
327
370
|
setTimeout(() => {
|
|
328
|
-
console.log('连接异常,尝试重新连接。。。。')
|
|
329
371
|
this.initWebSocket()
|
|
330
372
|
this.lockReconnect = false
|
|
331
|
-
},
|
|
373
|
+
}, delay)
|
|
332
374
|
},
|
|
333
375
|
// 初始化
|
|
334
376
|
initWebSocket () {
|
|
335
|
-
|
|
377
|
+
// 清理旧连接
|
|
378
|
+
if (this.$socket) {
|
|
379
|
+
this.$socket.close()
|
|
380
|
+
this.$socket = null
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// 自动检测协议
|
|
384
|
+
let protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
|
385
|
+
let path = `${protocol}//${location.host}/ws?token=${this.$login.f.id}`
|
|
386
|
+
|
|
336
387
|
try {
|
|
337
388
|
if ('WebSocket' in window) {
|
|
338
|
-
|
|
389
|
+
console.log(`正在连接WebSocket: ${path}`)
|
|
339
390
|
vue.prototype.$socket = new WebSocket(path)
|
|
391
|
+
|
|
392
|
+
// 检查WebSocket是否创建成功
|
|
393
|
+
if (this.$socket) {
|
|
394
|
+
this.$socket.onopen = this.onOpen // 连接成功
|
|
395
|
+
this.$socket.onmessage = this.onMessage // 收到消息时回调
|
|
396
|
+
this.$socket.onclose = this.onClose // 连接关闭时回调
|
|
397
|
+
this.$socket.onerror = this.onError // 通讯异常
|
|
398
|
+
} else {
|
|
399
|
+
console.error('WebSocket创建失败: $socket为null')
|
|
400
|
+
this.reconnect()
|
|
401
|
+
}
|
|
340
402
|
} else {
|
|
341
|
-
|
|
403
|
+
console.error('浏览器不支持WebSocket')
|
|
404
|
+
this.$showMessage('您的浏览器不支持websocket的协议,建议使用新版谷歌浏览器,请勿使用IE浏览器,360浏览器请使用极速模式,不要使用兼容模式!')
|
|
342
405
|
}
|
|
343
406
|
} catch (e) {
|
|
407
|
+
console.error('WebSocket创建失败:', e)
|
|
408
|
+
// 确保$socket被清空
|
|
409
|
+
vue.prototype.$socket = null
|
|
344
410
|
this.reconnect()
|
|
345
411
|
}
|
|
346
|
-
|
|
347
|
-
this.$socket.onopen = this.onOpen // 连接成功
|
|
348
|
-
this.$socket.onmessage = this.onMessage // 收到消息时回调
|
|
349
|
-
this.$socket.onclose = this.onClose // 连接关闭时回调
|
|
350
|
-
this.$socket.onerror = this.onError // 通讯异常
|
|
351
412
|
},
|
|
352
413
|
// 通讯异常
|
|
353
|
-
onError () {
|
|
354
|
-
console.log('
|
|
355
|
-
|
|
414
|
+
onError (error) {
|
|
415
|
+
console.log('WebSocket通讯异常:', error)
|
|
416
|
+
// 只有在还没达到最大重连次数时才重连
|
|
417
|
+
if (this.connectNumber < this.maxReconnectCount) {
|
|
418
|
+
this.reconnect()
|
|
419
|
+
}
|
|
356
420
|
},
|
|
357
421
|
// 连接成功
|
|
358
422
|
onOpen () {
|
|
359
|
-
console.log('
|
|
423
|
+
console.log('WebSocket连接成功')
|
|
424
|
+
// 重置重连计数
|
|
425
|
+
this.connectNumber = 0
|
|
360
426
|
this.start()//每十秒发送心跳包,确保连接状态有效
|
|
361
427
|
},
|
|
362
428
|
// 收到消息时回调函数
|
|
@@ -381,10 +447,14 @@ let createWaterMark = function (userName) {
|
|
|
381
447
|
}
|
|
382
448
|
},
|
|
383
449
|
// 关闭连接时回调函数
|
|
384
|
-
onClose () {
|
|
385
|
-
console.log('
|
|
450
|
+
onClose (event) {
|
|
451
|
+
console.log('WebSocket连接断开, code:', event.code, 'reason:', event.reason)
|
|
386
452
|
this.reset()
|
|
387
|
-
|
|
453
|
+
|
|
454
|
+
// 只有在非正常关闭且未达到最大重连次数时才重连
|
|
455
|
+
if (event.code !== 1000 && this.connectNumber < this.maxReconnectCount) {
|
|
456
|
+
this.reconnect()
|
|
457
|
+
}
|
|
388
458
|
},
|
|
389
459
|
getOrderList(){
|
|
390
460
|
let times = this.config.times * 60 * 1000
|
|
@@ -660,6 +730,26 @@ let createWaterMark = function (userName) {
|
|
|
660
730
|
})
|
|
661
731
|
}
|
|
662
732
|
},
|
|
733
|
+
beforeDestroy() {
|
|
734
|
+
// 清理定时器
|
|
735
|
+
if (this.interval) {
|
|
736
|
+
clearInterval(this.interval)
|
|
737
|
+
this.interval = null
|
|
738
|
+
}
|
|
739
|
+
if (this.intervaltx) {
|
|
740
|
+
clearInterval(this.intervaltx)
|
|
741
|
+
this.intervaltx = null
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// 清理WebSocket相关
|
|
745
|
+
this.reset()
|
|
746
|
+
if (this.$socket) {
|
|
747
|
+
this.$socket.close(1000, '页面关闭')
|
|
748
|
+
this.$socket = null
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
console.log('组件销毁,已清理所有资源')
|
|
752
|
+
},
|
|
663
753
|
computed: {
|
|
664
754
|
orgpathnames(){
|
|
665
755
|
let names = this.$login.f.name
|