js websocket封装类

发布日期:2018-01-04 15:59:02
/*
var websocket=new WebClient();//实例化对象
1、连接
可通过调用websocket对象中connect方法进行连接
如方法:connect(url,msgbak,openbak,errorbak,closebak)
url表示连接地址,必须以ws://开头的连接,如:ws://localhost:8080/HTML5WebSocekt/websocket
msgbak表示回调函数,就是当网络上有数据时,会自动调用此回调函数
openbak表示回调函数,就是当网络连接成功时,会自动调用此回调函数
errorbak表示回调函数,就是当网络连接异常时,会自动调用此回调函数
closebak表示回调函数,就是当网络关闭连接时,会自动调用此回调函数
如以下四个函数,可通过:websocket.connect("ws://localhost:8080/HTML5WebSocekt/websocket",onMessage,onOpen,onError,onClose);进行连接
function onMessage(data) {
document.getElementById('messages').innerHTML+= '<br />' + data;
}
function onOpen(event) {
document.getElementById('messages').innerHTML = '连接成功';
}


function onClose() {
document.getElementById('messages').innerHTML+= '<br />' + "关闭连接";
}
function onError(event) {
document.getElementById('messages').innerHTML+= '<br />' + "连接异常";
}


2、判断是否连接
可通过websocket对象中isConn()方法来判断连接,返回true表示已连接,返回false表示未连接


3、发送数据
可通过websocket对象中sendData()方法来发送数据,如:websocket.sendData('hello');


4、关闭连接
可通过websocket对象中close()方法来断开连接,如:websocket.close();






function SendData() {
websocket.sendData('hello');
}


function Connect()
{
var url="ws://localhost:8080/HTML5WebSocekt/websocket";
websocket.connect(url,onMessage,onOpen,onError,onClose);
}
function Close() {
websocket.close();
}
*/


function WebClient(){
WebClient.prototype.isopen=false;
    this.webSocket=null;
    this.connectUrl=function(url){
    this.webSocket = new WebSocket(url);
        this.webSocket.onerror = function(event) {
            WebClient.prototype.callerrorbak(event);
        }
        this.webSocket.onopen = function(event) {
        WebClient.prototype.setConnFlag(true);
            WebClient.prototype.callopenbak(event);
        }
        this.webSocket.onclose = function(event) {
            WebClient.prototype.callclosebak(event);
            WebClient.prototype.unInit();
        }
        this.webSocket.onmessage=function(event) {
            WebClient.prototype.callmsgbak(event.data);
        }
        return true;
    };
    if(typeof WebSocket._callmsgbak=="undefined"){
        WebClient.prototype.callmsgbak=function(data){


        }
        WebClient._callmsgbak=true;
    }
    if(typeof WebClient._callopenbak=="undefined"){
        WebClient.prototype.callopenbak=function(data){


        }
        WebClient._callopenbak=true;
    }
    if(typeof WebClient._callerrorbak=="undefined"){
        WebClient.prototype.callerrorbak=function(data){


        }
        WebClient._callerrorbak=true;
    }
    if(typeof WebClient._callclosebak=="undefined"){
        WebClient.prototype.callclosebak=function(data){


        }
        WebClient._callclosebak=true;
    }
    if(typeof WebClient._isConn=="undefined"){
        WebClient.prototype.isConn=function(data){
            return WebClient.prototype.isopen;
        }
        WebClient._isConn=true;
    }
    if(typeof WebClient._setConnFlag=="undefined"){
        WebClient.prototype.setConnFlag=function(flag){
        WebClient.prototype.isopen=flag;
        }
        WebClient._setConnFlag=true;
    }
    if(typeof WebClient._connect=="undefined"){
        WebClient.prototype.connect=function(url,msgbak,openbak,errorbak,closebak){
            WebClient.prototype.callmsgbak=msgbak;
            WebClient.prototype.callopenbak=openbak;
            WebClient.prototype.callerrorbak=errorbak;
            WebClient.prototype.callclosebak=closebak;
            if(this.webSocket==null)//表示连接成功
            {
            this.connectUrl(url);
            }
            else if(this.webSocket.readyState==3)
            {
            this.webSocket=null;
            this.connectUrl(url);
            }
            if(this.webSocket.readyState!=1)
            {
            return false;
            }
            return true;
        }
        WebClient._connect=true;
    }


    if(typeof WebClient._sendData=="undefined"){
        WebClient.prototype.sendData=function(data){
            if(WebClient.prototype.isConn()&&this.webSocket!=null)
            {
                this.webSocket.send(data);
                return true;
            }
            else
            {
                return false;
            }
        }
        WebClient._sendData=true;
    }
    if(typeof WebClient._close=="undefined"){
        WebClient.prototype.close=function(data){
            this.webSocket.close(1000,"Closeing");
            WebClient.prototype.isopen=false;
            this.webSocket=null;
            return true;
        }
        WebClient._close=true;
    }
    if(typeof WebClient._uninit=="undefined"){
        WebClient.prototype.unInit=function(data){
        WebClient.prototype.isopen=false;
            this.webSocket=null;
            return true;
        }
        WebClient._uninit=true;
    }
}