[javascript] js websocket断线重连库ReconnectingWebSocket

摘要:
当一个websocket被连接时,它会受到网络的影响,或者服务器长时间关闭通信。自己编写断开连接重新连接机制很麻烦。您可以使用js库ReconnectingWebSocket。js文件https://github.com/joewalnes/reconnecting-websocket/直接下载min文件,并在导入后使用它。您只需要用重新连接替换h5的本机websocket

websocket在连接的时候 , 受网络影响

或者长时间没有通信被服务端关闭 , 都需要断线重连机制

自己写断线重连比较麻烦 , 可以使用这个js库 ReconnectingWebSocket.js

 https://github.com/joewalnes/reconnecting-websocket/    直接下载min文件 , 引入就可以

使用的时候只需要把h5的原生websocket 替换成 ReconnectingWebSocket , 其他一切照旧

比如: 这是在vue中使用 this.socket就是全局的ReconnectingWebSocket对象 , 其他回调函数也是定义到vue的method上了

            this.socket = new ReconnectingWebSocket("xxxxxx");//创建Socket实例
            this.socket.debug = true;
            this.socket.timeoutInterval = 10000;//连接超时时间
            this.socket.reconnectInterval=5000;//重连间隔时间
            this.socket.maxReconnectInterval = 600000;//最大重连间隔时间
            this.socket.maxReconnectAttempts = 10;//最大重连尝试次数
            this.socket.onmessage = this.OnMessage;
            this.socket.onopen = this.OnOpen;
            this.socket.onerror = this.OnError;
            this.socket.onclose = this.OnClose;

[javascript] js websocket断线重连库ReconnectingWebSocket第1张

 超过一分钟没有任何通信 , 会中断 , 然后自动重连

免责声明:文章转载自《[javascript] js websocket断线重连库ReconnectingWebSocket》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇区块链学习5:智能合约Smart contract原理及发展历程科普知识k8s健康检查(七)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何搭建一个vue项目(完整步骤)

转载:https://www.cnblogs.com/hellman/p/10985377.html 一、安装node环境   1、下载地址为:https://nodejs.org/en/   2、检查是否安装成功:如果输出版本号,说明我们安装node环境成功      3、为了提高我们的效率,可以使用淘宝的镜像:http://npm.taobao.or...

js实现四舍五入Math.round与toFixed的比较

1 1、toFixed2 var total = 12.345; 3 //四舍五入,保留两位小数 4 var twoPoint = total.toFixed(2); //括号中的num即为保留的位数 5 console.log(twopoint); //12.35 6 7 但是...

驱动开发之 用DeviceIoControl实现应用程序与驱动程序通信

Ring3测试程序:http://blog.csdn.net/zj510/article/details/8216321 1.readfile和writefile可以实现应用程序与驱动程序通信,另外一个Win32 API 是DeviceIoControl。 应用程序自定义一中IO控制码,然后调用DeviceIoControl函数,IO管理器会产生一个Maj...

php解码js使用escape转码的函数

/** * 功能和js unescape函数,解码经过escape编码过的数据 * @param $str */ function unescape($str) { $ret = ''; $len = strlen($str); for ($i = 0; $i < $len; $i ++) {...

Vue 侦听器 watch

1. 侦听器 watch Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性 当属性发生改变时,自动触发属性对应的侦听器。 当需要在数据变化时执行异步或开销较大的操作时,这个方式是最有用的。 2. 基础用法 当 msg 属性的值发生改变时,就会触发侦听器的执行 <div id="app"> <input...

在 Vue 项目中(vue-cli2,vue-cli3)使用 pug 简化 HTML 的编写

使用 pug 的原因: 使得 HTML 写起了来更加清晰和快捷 用法: Vue 的用法没有变化: <template lang="pug"> transition(name="sider") div.hello h3 {{msg}} p(:style="{color:'#000'}", :htmlData="...