Webscoket的使用以及优化

最近项目中用到了webscoket,直接把代码放到这里,有问题互相讨论
var ws = null;  
var url = null;  
var transports = [];  
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000;   

function setConnected(connected) {  
    document.getElementById('connect').disabled = connected;  
    document.getElementById('disconnect').disabled = !connected;  
    document.getElementById('echo').disabled = !connected;  
}  

function connect() {

    //ws = new SockJS("http://127.0.0.1:8080/sym_cop/s1/sockjs/websck?k=8377bf3e-af81-4eb4-816e-b7e05ef3f774");
     if ('WebSocket' in window) {
        var ipUrl = window.location.host;
        ipUrl = ipUrl.split(':')[0];
        ws= new WebSocket("ws://"+ ipUrl +":8020/s1/websck?k="+ $.cookie('k'));  
        // console.log("http://127.0.0.1:8080/sym_cop/s1/websck");  
        }

    ws.onopen = function () {  

       // ws.send(message);  

    };  
    ws.onmessage = function (event) {  

         log(event.data);  

    };  

    ws.onclose = function (event) {  

        log('connection closed.');  
        // log(event);
        var  a=Date.parse(new Date());
        a=a/1000;
        if(a-timestamp>10){            
            connect();
        }else{
            setTimeout(function(){
                connect();
            },10000)
        }
        timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;   
    };

}  

function disconnect() {  
    if (ws != null) {  
        ws.close();  
        ws = null;  
    }  
    setConnected(false);  
}  
setInterval(function(){
    if(ws){
         ws.send('heart');
    }
},30000)
function echo() {  
    if (ws != null) {  
        var message = document.getElementById('message').value;  
        //log('Sent: ' + message);  
        ws.send(message); 
    } else {  
        alert('connection not established, please connect.');  
    }  
}  

function updateUrl(urlPath) {  
    if (urlPath.indexOf('sockjs') != -1) {  
        url = urlPath;  
        document.getElementById('sockJsTransportSelect').style.visibility = 'visible';  
    }  
    else {  
        if (window.location.protocol == 'http:') {
            url = 'ws://' + window.location.host + urlPath;  
        } else {  
            url = 'wss://' + window.location.host + urlPath;  
        }  
        document.getElementById('sockJsTransportSelect').style.visibility = 'hidden';  
    }  
}  

function updateTransport(transport) {  
    // alert(transport);  
    transports = (transport == 'all') ?  [] : [transport];  
}  

function log(message) {
    if(message == 'heart'){
        return
    }
    if(message == 'connection closed.'){

    }else if(message == "Server:connected OK!"){
        setTimeout(function(){
            getPouple();
        },300)
    }else{
        setTimeout(function(){
            getPouple();
        },300)
    }

} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值