HTML5 WebSocket 封装
来源:互联网 发布:java svn版本号 编辑:程序博客网 时间:2024/06/07 07:08
/*var websocket=new WebClient();//实例化对象1、连接可通过调用websocket对象中connect方法进行连接如方法:connect(url,msgbak,openbak,errorbak,closebak)url表示连接地址,必须以ws://开头的连接,如:ws://localhost:8080/HTML5WebSocekt/websocketmsgbak表示回调函数,就是当网络上有数据时,会自动调用此回调函数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; }}
0 0
- HTML5 WebSocket 封装
- WebSocket 封装
- 【Html5 WebSocket】WebSocket事件
- html5 websocket
- html5 websocket
- HTML5 WebSocket
- HTML5 webSocket
- HTML5 WebSocket
- html5 websocket
- html5 WebSocket
- html5 websocket
- HTML5 WebSocket
- HTML5 WebSocket
- HTML5 WebSocket
- html5 websocket
- HTML5 WebSocket
- HTML5 WebSocket
- html5 WebSocket
- winform 快捷键的两种实现方法
- nginx1.9 在centos7.2下报Failed to restart nginx.service: Unit nginx.service failed to load: No 。。错误
- 代码整洁—代码维护
- Linux查看CPU信息的.sh文件
- 8.3 魔鬼(no.21~no.30)
- HTML5 WebSocket 封装
- 欢迎使用CSDN-markdown编辑器
- Android Studio 缺少Android支持
- HTML基础之表单设计
- Adding Animations之Using ViewPager for Screen Slides
- lintcode栅栏染色
- f4的待机模式和唤醒代码
- CodeForces 115A Party
- 关于SIGCHLD信号的处理