当前位置: 首页 > news >正文

Js WebSocket类,收发Json,带心跳,断线重连

如题
心跳:4秒发一次
断线:2秒后自动重连
收发:发送和返回json,处理粘包断包等情况,json字符串最大长度9999
缓存:未连接时,自动缓存100个包,当连接时会自动发出

JS代码

var MyWebSocket = {ws : null,isConnected : false,strLast : "",isDebug : true,url : "",//msreconnectTimeout : 2000,sendBuffMaxSize : 100,arrSendBuff : [],timer : 0,connect : function(url){this.url = url;var that = this;if( this.ws != null ){this.ws.onopen = null;this.ws.onmessage = null;this.ws.onclose = null;this.ws.onerror = null;}if( this.timer==0 ){timer = setInterval( this.heart, 4000, this );}this.ws = new WebSocket(url);this.ws.onopen = function(){that.isConnected = true;//当WebSocket创建成功时,触发onopen事件that.log("open");that.ws.send("0002{}"); //将消息发送到服务端that.sendBuffJson();}this.ws.onmessage = function(e){that.log(e.data);that.strLast += e.data;var strlen = that.strLast.length;if( strlen > 4 ){var len = parseInt( "0x" + that.strLast.substr(0, 4));if( len+4 <= strlen ){var s = that.strLast.substr(4, len+4);that.strLast = that.strLast.substr(len+4);that.log("msg come");that.log(s);if( that.onMsgCome != null ){this.onMsgCome(JSON.parse(s));}}}}this.ws.onclose = function(e){//当客户端收到服务端发送的关闭连接请求时,触发onclose事件that.log("close");that.isConnected = false;that.reconnect();}this.ws.onerror = function(e){//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件that.log(error);}},reconnect : function(){if( this.reconnectTimeout > 0 ){setTimeout(this.doReconnect, this.reconnectTimeout, this);}else this.doReconnect(this);},doReconnect : function(that){that.connect(that.url);},sendBuffJson : function(){var len = this.arrSendBuff.length;for( var i=0; i<len; i++ ){var json = this.arrSendBuff[i];this.send(json);}return len;},heart : function(that){if( !that.isConnected ) return;that.timerNum++;if( that.timerNum > that.sendNum ){that.log("heart");that.ws.send("0000");}},timerNum : 1,sendNum : 1,send : function(json){if( !this.isConnected ){if( this.arrSendBuff.length < this.sendBuffMaxSize ){this.arrSendBuff.push(json);}return;}this.sendNum = this.timerNum + 1;var s = JSON.stringify(json);var prev = "0000" + s.length.toString(16);prev = prev.substr(prev.length-4);s = prev + s;this.ws.send(s);},log : function(s){if( this.isDebug ) console.log(s);},//信息回调回调函数onMsgCome : null,
}

测试代码

<!DOCTYPE html>
<html>
<head><meta charset=utf-8 /><title>MyWebSocket</title>
</head>
<script type="text/javascript"> </script>
<body><script src="MyWebSocket.js"></script><script>var mw = Object.create(MyWebSocket);mw.connect("ws://127.0.0.1:8888");mw.onMsgCome = function(json){console.log(json);}setInterval(xx, 3000);function xx(){var json = {};json.url = "xx";json.data = {};mw.send(json);}</script></body></html>
http://www.lryc.cn/news/261242.html

相关文章:

  • VBA技术资料MF96:单字段多条件高级筛选
  • 电子取证中Chrome各版本解密Cookies、LoginData账号密码、历史记录
  • Axure元件基本介绍进阶
  • 安卓11添加切换以太网动态静态方法
  • 初级数据结构(五)——树和二叉树的概念
  • pdf读取内容缺失(漏字/文字丢失)问题
  • c#面试基础语法——现有⼀个整数number,请写⼀个⽅法判断这个整数是否是2的N次⽅
  • 27系列DGUS智能屏发布:可实时播放高清模拟信号摄像头视频
  • YOLOv8改进 | 2023主干篇 | 替换LSKNet遥感目标检测主干 (附代码+修改教程+结构讲解)
  • 【工具】VUE 前端列表拖拽功能代码
  • 人工智能与量子计算:开启未知领域的智慧之旅
  • 2023了,前端实现AI电子秤思路分析
  • CSS学习
  • Flask基本用法:一个HelloWorld,搭建服务、发起请求
  • Tomcat-安装部署(源码包安装)
  • 【Hadoop_06】MapReduce的概述与wc案例
  • Qt点击子窗口时父窗口标题栏高亮设计思路
  • 掌握iText:轻松处理PDF文档-高级篇-添加水印
  • 深度学习基本概念
  • 2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
  • 基于ssm金旗帜文化培训学校网站的设计与开发论文
  • 【Java】猜数字小游戏
  • 汽车EDI:Chrysler EDI项目案例
  • Locust:可能是一款最被低估的压测工具
  • 用23种设计模式打造一个cocos creator的游戏框架----(十八)责任链模式
  • 100天精通风控建模(原理+Python实现)——第9天:风控建模中为什么需要特征工程?
  • 【PHP】计算某个特定时间戳距离现在的天数
  • lv12 uboot移植深化 9
  • 大数据与深度挖掘:如何在数字营销中与研究互动
  • xtu oj 1327 字符矩阵