websocket代码提交
This commit is contained in:
@@ -53,7 +53,7 @@ export default class SocketService {
|
||||
// 1.连接服务端失败
|
||||
// 2.当连接成功之后, 服务器关闭的情况
|
||||
this.ws.onclose = () => {
|
||||
console.log('连接服务端失败');
|
||||
console.log('连接webSocket服务端关闭');
|
||||
this.connected = false;
|
||||
this.connectRetryCount++;
|
||||
this.clearHeartbeat();
|
||||
@@ -76,7 +76,7 @@ export default class SocketService {
|
||||
if(event.data == 'over') {
|
||||
//心跳消息处理
|
||||
this.lastResponseHeartTime = Date.now();
|
||||
|
||||
this.updateLastActivityTime(); // 收到心跳响应时更新活动时间
|
||||
}else {
|
||||
let message: { [key: string]: any };
|
||||
try {
|
||||
@@ -106,7 +106,7 @@ export default class SocketService {
|
||||
|
||||
startHeartbeat() {
|
||||
const _this = this
|
||||
const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},10000)})()']));
|
||||
const url = window.URL.createObjectURL(new Blob(['(function(e){setInterval(function(){this.postMessage(null)},9000)})()']));
|
||||
this.work = new Worker(url)
|
||||
this.work.onmessage = function(e){
|
||||
//判断多久没收到心跳响应
|
||||
@@ -114,6 +114,7 @@ export default class SocketService {
|
||||
if(_this.lastActivityTime - _this.lastResponseHeartTime > 30000){
|
||||
//说明已经三轮心跳没收到回复了,关闭检测,提示用户。
|
||||
ElMessage.error("业务主体模块发生未知异常,请尝试重新启动!");
|
||||
_this.clearHeartbeat();
|
||||
return;
|
||||
}
|
||||
_this.sendHeartbeat();
|
||||
@@ -172,6 +173,6 @@ export default class SocketService {
|
||||
if (this.connected) {
|
||||
this.ws.close()
|
||||
}
|
||||
console.log('WS关闭中..');
|
||||
console.log('执行WS关闭命令..');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user