标签:else etc 关闭 网络 var idt false off location
在页面加载前 导入两个js;
var socketStatus=false; var t2; //重连 机构 function tanchuang(){ Offline.check(); //为false 时执行 if(!socketStatus){ AlertSvc.layAlerterr("当前网络不佳,请稍后再试!"); if(Offline.state === ‘up‘ && websocket.reconnectAttempts>websocket.maxReconnectInterval){ window.location.reload(); } } } //websock连接事件 function websocketConnect(add) { if (add === undefined) { add = cip; } if (‘WebSocket‘ in window) { websocket = new ReconnectingWebSocket(add); } else if (‘MozWebSocket‘ in window) { websocket = new MozWebSocket(add); } else { websocket = new SockJS(add); } //连接成功建立的回调方法 websocket.onopen = function() { //重连 机构 socketStatus=true; t2=setInterval(tanchuang,3000); }; //接收到消息的回调方法 websocket.onmessage = function(event) { }; //连接发生错误的回调方法\ websocket.onerror = function() { socketStatus=false; console.log("WebSocket连接错误"); }; //连接关闭的回调方法2017-08-15 websocket.onclose = function() { socketStatus=false; console.log("WebSocket连接关闭"); }; //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function() { if(event.clientX > document.body.clientWidth && event.clientY < 0 || event.altKey) { closeWebSocket(); } };
<script src="lib/offline.js"></script>
<script src="lib/reWebsocket.js"></script>
2017-08-15 12:11:29
标签:else etc 关闭 网络 var idt false off location
原文地址:http://www.cnblogs.com/wrrISjs/p/7364429.html