|
@@ -4,6 +4,7 @@ import {
|
|
|
} from '@/utils/localStorage';
|
|
|
window.sessionTime = new Date();
|
|
|
var socketTaskList = []; //websocket 待发送任务列表
|
|
|
+var socketInter = null;
|
|
|
/**
|
|
|
* websocket配置项
|
|
|
* @type {{serverTimeoutObj: null, timeoutObj: null, timeoutnum: null, lockReconnect: boolean, ws: null, params: {houseId: null, openid: null, userid: null}, timeout: number}}
|
|
@@ -56,7 +57,7 @@ function initWebsocket() {
|
|
|
function websocketonopen() {
|
|
|
let data = initPage();
|
|
|
console.log('建立ws连接', data)
|
|
|
- start();
|
|
|
+ start();
|
|
|
//发送链接身份数据
|
|
|
connectSend(data.openId, data.userId, data.houseId);
|
|
|
}
|
|
@@ -68,7 +69,7 @@ function websocketonopen() {
|
|
|
function websocketonmessage(e) {
|
|
|
console.log('客户端接收服务端数据时触发', e);
|
|
|
//收到服务器信息,心跳重置
|
|
|
- reset();
|
|
|
+ // reset();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -86,6 +87,8 @@ function websocketonerror() {
|
|
|
function websocketclose(e) {
|
|
|
//关闭
|
|
|
console.log("断开连接", e);
|
|
|
+ socketInter && clearInterval(socketInter);//清空心跳发送
|
|
|
+ socketInter = null;
|
|
|
//重连
|
|
|
reconnect();
|
|
|
}
|
|
@@ -234,6 +237,8 @@ function reconnect() {
|
|
|
initWebsocket()
|
|
|
wsConfig.lockReconnect = false
|
|
|
}, 5000)
|
|
|
+ socketInter && clearInterval(socketInter);//清空心跳发送
|
|
|
+ socketInter = null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -242,36 +247,49 @@ function reconnect() {
|
|
|
function reset() {
|
|
|
console.log('重启心跳')
|
|
|
//重置心跳
|
|
|
- clearTimeout(wsConfig.timeoutObj);
|
|
|
- //清除时间
|
|
|
- clearTimeout(wsConfig.serverTimeoutObj);
|
|
|
+ // clearTimeout(wsConfig.timeoutObj);
|
|
|
+ // //清除时间
|
|
|
+ // clearTimeout(wsConfig.serverTimeoutObj);
|
|
|
//重启心跳
|
|
|
- start();
|
|
|
+ wsSendHeartBeat();
|
|
|
+}
|
|
|
+// socket 心跳发送
|
|
|
+function wsSendHeartBeat() {
|
|
|
+ let ws = wsConfig.ws;
|
|
|
+ // var data = ["\n"];//心跳的数据格式
|
|
|
+ if(!socketInter){
|
|
|
+ console.warn('***ws-开启WebSocket心跳***');
|
|
|
+ //5秒钟发送一次心跳
|
|
|
+ socketInter = setInterval(()=>{
|
|
|
+ // console.warn("***ws-SendHeartBeat-尝试心跳发送:");
|
|
|
+ //这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
|
+ console.warn('***ws-心跳***');
|
|
|
+ if (ws.readyState == 1) {
|
|
|
+ //如果连接正常
|
|
|
+ ws.send("heartCheck"); //这里可以自己跟后端约定
|
|
|
+ } else {
|
|
|
+ //否则重连
|
|
|
+ reconnect();
|
|
|
+ }
|
|
|
+ },5000)
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 开启ws连接
|
|
|
*/
|
|
|
function start() {
|
|
|
let ws = wsConfig.ws;
|
|
|
//开启心跳
|
|
|
- console.log("开启心跳", wsConfig,ws.readyState);
|
|
|
+ // console.log("开启心跳", wsConfig,ws.readyState);
|
|
|
wsConfig.timeoutObj && clearTimeout(wsConfig.timeoutObj);
|
|
|
wsConfig.serverTimeoutObj && clearTimeout(wsConfig.serverTimeoutObj);
|
|
|
wsConfig.timeoutObj = setTimeout(function() {
|
|
|
- //这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
|
- if (ws.readyState == 1) {
|
|
|
- //如果连接正常
|
|
|
- ws.send("heartCheck"); //这里可以自己跟后端约定
|
|
|
- } else {
|
|
|
- //否则重连
|
|
|
- reconnect();
|
|
|
- }
|
|
|
wsConfig.serverTimeoutObj = setTimeout(function() {
|
|
|
//超时关闭
|
|
|
ws.close();
|
|
|
}, wsConfig.timeout);
|
|
|
}, wsConfig.timeout);
|
|
|
+ wsSendHeartBeat();//开始心跳
|
|
|
}
|
|
|
var util = {
|
|
|
dateFormat(date, fmt) {
|