|
@@ -31,18 +31,21 @@ var WebSocketHeartCheck = function(){
|
|
|
this.timeoutObj = null;
|
|
|
this.serverTimeoutObj = null;
|
|
|
this.reset = function(){
|
|
|
- console.info("reset。。。");
|
|
|
clearTimeout(this.timeoutObj);
|
|
|
clearTimeout(this.serverTimeoutObj);
|
|
|
return this;
|
|
|
}
|
|
|
this.start = function(){
|
|
|
+ if(loginflag){
|
|
|
+ return;
|
|
|
+ }
|
|
|
var self = this;
|
|
|
this.timeoutObj = setTimeout(function(){
|
|
|
//这里发送一个心跳,后端收到后,返回一个心跳消息,
|
|
|
//onmessage拿到返回的心跳就说明连接正常
|
|
|
- console.info("send HeartBeat");
|
|
|
- ws.send("HeartBeat");
|
|
|
+ if(ws.readyState==1){
|
|
|
+ ws.send("HeartBeat");
|
|
|
+ }
|
|
|
self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
|
|
|
ws.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
|
|
|
}, self.timeout);
|
|
@@ -63,28 +66,27 @@ var createWebSocket = function(wsUrl){
|
|
|
//
|
|
|
var initEventHandle = function(){
|
|
|
ws.onmessage = function(e){
|
|
|
- console.info(e.data);
|
|
|
- console.info("recive。。。");
|
|
|
if(e.data==""){
|
|
|
return
|
|
|
- }else if(e.data=="\"ok\""){//重新发送shareid
|
|
|
+ }else if(e.data=="ok"){//重新发送shareid
|
|
|
clearInterval(sendMsgIntervalObj);
|
|
|
- console.info(sendMsgIntervalObj)
|
|
|
+ sendMsgIntervalObj = null;
|
|
|
return
|
|
|
- }else if(e.data=="\"HeartBeat\""){//心跳检测
|
|
|
+ }else if(e.data=="HeartBeat"){//心跳检测
|
|
|
//如果获取到消息,心跳检测重置
|
|
|
//拿到任何消息都说明当前连接是正常的
|
|
|
webSocketHeartCheck.reset().start();
|
|
|
+ return;
|
|
|
}
|
|
|
//用户登录
|
|
|
logic(e.data,mynum);
|
|
|
}
|
|
|
ws.onerror = function(e){
|
|
|
- console.info("onerror");
|
|
|
+ //console.info("onerror");
|
|
|
reconnect(wsUrl);
|
|
|
}
|
|
|
ws.onclose = function () {
|
|
|
- console.info("onclose");
|
|
|
+ //console.info("onclose");
|
|
|
reconnect(wsUrl);
|
|
|
}
|
|
|
ws.onopen = function(e) {
|
|
@@ -94,11 +96,12 @@ var initEventHandle = function(){
|
|
|
}
|
|
|
//
|
|
|
function reconnect(url) {
|
|
|
- if(webSocketHeartCheck.lockReconnect) return;
|
|
|
+ if(webSocketHeartCheck.lockReconnect || loginflag){
|
|
|
+ return;
|
|
|
+ }
|
|
|
webSocketHeartCheck.lockReconnect = true;
|
|
|
//没连接上会一直重连,设置延迟避免请求过多
|
|
|
setTimeout(function () {
|
|
|
- console.info("reconnect。。。");
|
|
|
createWebSocket(url);
|
|
|
webSocketHeartCheck.lockReconnect = false;
|
|
|
if(sendMsgIntervalObj == null){
|
|
@@ -123,10 +126,7 @@ var JYLogin = function(num){
|
|
|
}
|
|
|
var SendMsg = function(){
|
|
|
//向后台发送websocket数据
|
|
|
- console.info("-------------000000000");
|
|
|
if (window["WebSocket"]){
|
|
|
- console.info("-------------11111111");
|
|
|
- console.info(ws.readyState);
|
|
|
if(ws.readyState==1){
|
|
|
console.log("发送:"+pageshareid+"___"+kopshareid)
|
|
|
ws.send(pageshareid+"___"+kopshareid);
|
|
@@ -251,6 +251,8 @@ var logic = function(infoData,num){
|
|
|
clearInterval(loginfg);
|
|
|
loginflag = true;
|
|
|
processpage(data.shareid,num);
|
|
|
+ //登录成功,停止心跳监测
|
|
|
+ webSocketHeartCheck.reset();
|
|
|
if(ws.readyState==1){
|
|
|
ws.send("close");
|
|
|
ws.close();
|