HTML5 WebSocket 封装

/*	
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;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值