websocket 重连解决方案(2)

var socketStatus=false;
var lineStatus=true;
var connectCount=0;
var t1;
var t2;
var flag1=true;
var flag2=true;
var flag3=true;
var p_click_num=0;
var p_click_num1=0;
var p_flag=true;
var p_ka=true;
var jinjileft=0;
var jinjiright;
var jjleftclick=true;
var    dgcleftclick=true;
var dgcleft=0;
var dgcright;
var p_next_click_0=true;
var p_next_click_1=true;
var deal=1;
var sessionId="";
var onMouseOutTime=0;
var backFirst=false;

// 判断网络状态
function isonline(){
    $.ajax({
        url: WEB_URL+"favicon.ico?_=" + new Date().getTime(),
        type: "GET",
        success:function(data){
           lineStatus=true;    
        },
        error:function(e){
           lineStatus=false;
        }
    })
}

function chkIn(){
  deal=0;
  onMouseOutTime=0;
 }


function chkOut(){
  onMouseOutTime=new Date().getTime();
}


function tanchuang(){
    if(!socketStatus){
        if(!lineStatus){
            isonline();
        }
        if(lineStatus && websocket.reconnectAttempts>websocket.maxReconnectAttempts){
            window.location.reload();
        }
        else if(lineStatus && websocket.reconnectAttempts<websocket.maxReconnectAttempts){
            $('.big_toast div').html('正在尝试重连...');
        }else{
            $('.big_toast div').html('网络连接已断开!');
        }
        $('.big_toast').css('left', '45%');
        $('.big_toast').fadeIn("fast");
        $('.big_toast').fadeOut(2000);
    //    buildSocket();
    }else{
        if(onMouseOutTime!=0){
                var currentTime=new Date().getTime();
                if((currentTime-onMouseOutTime)/1000>20){
                    deal=1;
                    backFirst=true;
                }
        }
        websocket.send("{'nurseId':5,'userId':44 }");
    }
}



var websocket;
buildSocket();
function buildSocket(){
        if ('WebSocket' in window) { 
            //websocket = new ReconnectingWebSocket("ws://127.0.0.1:8050/websocket/get/overview");
               
           websocket = new ReconnectingWebSocket("ws://127.0.0.1:8050/websocket/user/current/msg");
           // websocket = new ReconnectingWebSocket("ws://nurs4test.91ganlu.com:5552/websocket/user/current/msg");

        } else if ('MozWebSocket' in window) {
            //   websocket = new MozWebSocket("ws://192.168.1.114/websocket/get/all/data/rt");
            websocket = new MozWebSocket(WEBSOCKET_URL+"/websocket/get/overview");
        } else {
            //   websocket = new SockJS("http://192.168.1.114/sockjs/websocket/get/all/data/rt");
            websocket = new SockJS("http://nurse.91ganlu.com/websocket/get/overview");
                     
        }
        
}
websocket.onopen = function (evnt) {
    socketStatus=true;
    lineStatus = true;
    clearInterval(t1);//去掉定时器
    t2=setInterval(tanchuang,3000);
    // tanchuang();
};
websocket.onmessage = function (evnt) {
    var evntstr=evnt.data;
    var evntobj=JSON.parse(evntstr);
    var html="";
    //console.log(JSON.stringify(evntobj.data));
    console.log(evntobj.data);
};
websocket.onerror = function (evnt) {
    socketStatus=false;
    lineStatus = false;
};
websocket.onclose = function (evnt) {
    socketStatus=false;
    lineStatus = false;
};

$(function(){
    $('.logos').find('img').attr('src',LOGOSRC);
})

 

var socketStatus=false;var lineStatus=true;var connectCount=0;var t1;var t2;var flag1=true;var flag2=true;var flag3=true;var p_click_num=0;var p_click_num1=0;var p_flag=true;var p_ka=true;var jinjileft=0;var jinjiright;var jjleftclick=true;vardgcleftclick=true;var dgcleft=0;var dgcright;var p_next_click_0=true;var p_next_click_1=true;var deal=1;var sessionId="";var onMouseOutTime=0;var backFirst=false;
// 判断网络状态function isonline(){$.ajax({url: WEB_URL+"favicon.ico?_=" + new Date().getTime(),type: "GET",success:function(data){   lineStatus=true;},error:function(e){   lineStatus=false;}})}
function chkIn(){  deal=0;  onMouseOutTime=0; }

function chkOut(){  onMouseOutTime=new Date().getTime();}

function tanchuang(){if(!socketStatus){if(!lineStatus){isonline();}if(lineStatus && websocket.reconnectAttempts>websocket.maxReconnectAttempts){window.location.reload();}else if(lineStatus && websocket.reconnectAttempts<websocket.maxReconnectAttempts){$('.big_toast div').html('正在尝试重连...');}else{$('.big_toast div').html('网络连接已断开!');}$('.big_toast').css('left', '45%');$('.big_toast').fadeIn("fast");$('.big_toast').fadeOut(2000);//buildSocket();}else{    if(onMouseOutTime!=0){    var currentTime=new Date().getTime();    if((currentTime-onMouseOutTime)/1000>20){    deal=1;    backFirst=true;    }    }websocket.send("{'nurseId':5,'userId':44 }");}}


var websocket;buildSocket();function buildSocket(){if ('WebSocket' in window) { //websocket = new ReconnectingWebSocket("ws://127.0.0.1:8050/websocket/get/overview");          websocket = new ReconnectingWebSocket("ws://127.0.0.1:8050/websocket/user/current/msg");   // websocket = new ReconnectingWebSocket("ws://nurs4test.91ganlu.com:5552/websocket/user/current/msg");
} else if ('MozWebSocket' in window) {    //   websocket = new MozWebSocket("ws://192.168.1.114/websocket/get/all/data/rt");    websocket = new MozWebSocket(WEBSOCKET_URL+"/websocket/get/overview");} else {    //   websocket = new SockJS("http://192.168.1.114/sockjs/websocket/get/all/data/rt");    websocket = new SockJS("http://nurse.91ganlu.com/websocket/get/overview");             }}websocket.onopen = function (evnt) {socketStatus=true;lineStatus = true;clearInterval(t1);//去掉定时器t2=setInterval(tanchuang,3000);// tanchuang();};websocket.onmessage = function (evnt) {var evntstr=evnt.data;var evntobj=JSON.parse(evntstr);var html="";//console.log(JSON.stringify(evntobj.data));console.log(evntobj.data);};websocket.onerror = function (evnt) {socketStatus=false;lineStatus = false;};websocket.onclose = function (evnt) {socketStatus=false;lineStatus = false;};
$(function(){$('.logos').find('img').attr('src',LOGOSRC);})

 

posted @ 2017-06-20 15:58  Enast  阅读(309)  评论(0编辑  收藏  举报