websocket-heartbeat-js
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -0,1 +1,10 @@ | ||
<a name="1.0.8"></a> | ||
## 1.0.8 (2019-04-09) | ||
### Features | ||
* add a limit to the times of reconnection ([75c60af](https://github.com/zimv/websocket-heartbeat-js/commit/75c60af)) | ||
<a name="1.0.7"></a> | ||
@@ -2,0 +11,0 @@ ## 1.0.7 (2018-10-25) |
@@ -1,1 +0,1 @@ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var o=t();for(var n in o)("object"==typeof exports?exports:e)[n]=o[n]}}(window,function(){return function(e){var t={};function o(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,o),i.l=!0,i.exports}return o.m=e,o.c=t,o.d=function(e,t,n){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)o.d(n,i,function(t){return e[t]}.bind(null,i));return n},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=0)}([function(e,t,o){"use strict";function n(e){var t=e.url,o=e.pingTimeout,n=void 0===o?15e3:o,i=e.pongTimeout,r=void 0===i?1e4:i,c=e.reconnectTimeout,s=void 0===c?2e3:c,u=e.pingMsg,f=void 0===u?"heartbeat":u;this.opts={url:t,pingTimeout:n,pongTimeout:r,reconnectTimeout:s,pingMsg:f},this.ws=null,this.onclose=function(){},this.onerror=function(){},this.onopen=function(){},this.onmessage=function(){},this.onreconnect=function(){},this.createWebSocket()}Object.defineProperty(t,"__esModule",{value:!0}),n.prototype.createWebSocket=function(){try{this.ws=new WebSocket(this.opts.url),this.initEventHandle()}catch(e){throw this.reconnect(),e}},n.prototype.initEventHandle=function(){var e=this;this.ws.onclose=function(){e.onclose(),e.reconnect()},this.ws.onerror=function(){e.onerror(),e.reconnect()},this.ws.onopen=function(){e.onopen(),e.heartCheck()},this.ws.onmessage=function(t){e.onmessage(t),e.heartCheck()}},n.prototype.reconnect=function(){var e=this;this.lockReconnect||this.forbidReconnect||(this.lockReconnect=!0,this.onreconnect(),setTimeout(function(){e.createWebSocket(),e.lockReconnect=!1},this.opts.reconnectTimeout))},n.prototype.send=function(e){this.ws.send(e)},n.prototype.heartCheck=function(){this.heartReset(),this.heartStart()},n.prototype.heartStart=function(){var e=this;this.forbidReconnect||(this.pingTimeoutId=setTimeout(function(){e.ws.send(e.opts.pingMsg),e.pongTimeoutId=setTimeout(function(){e.ws.close()},e.opts.pongTimeout)},this.opts.pingTimeout))},n.prototype.heartReset=function(){clearTimeout(this.pingTimeoutId),clearTimeout(this.pongTimeoutId)},n.prototype.close=function(){this.forbidReconnect=!0,this.heartReset(),this.ws.close()},window&&(window.WebsocketHeartbeatJs=n),t.default=n}])}); | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o=e();for(var n in o)("object"==typeof exports?exports:t)[n]=o[n]}}(window,function(){return function(t){var e={};function o(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,o),i.l=!0,i.exports}return o.m=t,o.c=e,o.d=function(t,e,n){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)o.d(n,i,function(e){return t[e]}.bind(null,i));return n},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=0)}([function(t,e,o){"use strict";function n(t){var e=t.url,o=t.pingTimeout,n=void 0===o?15e3:o,i=t.pongTimeout,r=void 0===i?1e4:i,c=t.reconnectTimeout,s=void 0===c?2e3:c,u=t.pingMsg,p=void 0===u?"heartbeat":u,f=t.repeatLimit,a=void 0===f?null:f;this.opts={url:e,pingTimeout:n,pongTimeout:r,reconnectTimeout:s,pingMsg:p,repeatLimit:a},this.ws=null,this.repeat=0,this.onclose=function(){},this.onerror=function(){},this.onopen=function(){},this.onmessage=function(){},this.onreconnect=function(){},this.createWebSocket()}Object.defineProperty(e,"__esModule",{value:!0}),n.prototype.createWebSocket=function(){try{this.ws=new WebSocket(this.opts.url),this.initEventHandle()}catch(t){throw this.reconnect(),t}},n.prototype.initEventHandle=function(){var t=this;this.ws.onclose=function(){t.onclose(),t.reconnect()},this.ws.onerror=function(){t.onerror(),t.reconnect()},this.ws.onopen=function(){t.repeat=0,t.onopen(),t.heartCheck()},this.ws.onmessage=function(e){t.onmessage(e),t.heartCheck()}},n.prototype.reconnect=function(){var t=this;this.opts.repeatLimit>0&&this.opts.repeatLimit<=this.repeat||this.lockReconnect||this.forbidReconnect||(this.lockReconnect=!0,this.repeat++,this.onreconnect(),setTimeout(function(){t.createWebSocket(),t.lockReconnect=!1},this.opts.reconnectTimeout))},n.prototype.send=function(t){this.ws.send(t)},n.prototype.heartCheck=function(){this.heartReset(),this.heartStart()},n.prototype.heartStart=function(){var t=this;this.forbidReconnect||(this.pingTimeoutId=setTimeout(function(){t.ws.send(t.opts.pingMsg),t.pongTimeoutId=setTimeout(function(){t.ws.close()},t.opts.pongTimeout)},this.opts.pingTimeout))},n.prototype.heartReset=function(){clearTimeout(this.pingTimeoutId),clearTimeout(this.pongTimeoutId)},n.prototype.close=function(){this.forbidReconnect=!0,this.heartReset(),this.ws.close()},window&&(window.WebsocketHeartbeatJs=n),e.default=n}])}); |
@@ -20,3 +20,4 @@ /** | ||
reconnectTimeout = 2000, | ||
pingMsg = 'heartbeat' | ||
pingMsg = 'heartbeat', | ||
repeatLimit = null | ||
}){ | ||
@@ -28,5 +29,7 @@ this.opts ={ | ||
reconnectTimeout: reconnectTimeout, | ||
pingMsg: pingMsg | ||
pingMsg: pingMsg, | ||
repeatLimit: repeatLimit | ||
}; | ||
this.ws = null;//websocket实例 | ||
this.repeat = 0; | ||
@@ -62,2 +65,3 @@ //override hook function | ||
this.ws.onopen = () => { | ||
this.repeat = 0; | ||
this.onopen(); | ||
@@ -76,4 +80,6 @@ //心跳检测重置 | ||
WebsocketHeartbeatJs.prototype.reconnect = function(){ | ||
if(this.opts.repeatLimit>0 && this.opts.repeatLimit <= this.repeat) return;//limit repeat the number | ||
if(this.lockReconnect || this.forbidReconnect) return; | ||
this.lockReconnect = true; | ||
this.repeat++;//必须在lockReconnect之后,避免进行无效计数 | ||
this.onreconnect(); | ||
@@ -80,0 +86,0 @@ //没连接上会一直重连,设置延迟避免请求过多 |
{ | ||
"name": "websocket-heartbeat-js", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "websocket heartbeat", | ||
@@ -9,2 +9,3 @@ "main": "./dist/index.js", | ||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", | ||
"build": "node_modules/webpack/bin/webpack.js", | ||
"lint": "eslint .", | ||
@@ -11,0 +12,0 @@ "fix": "eslint --fix ." |
@@ -82,6 +82,6 @@ [![Build Status](https://travis-ci.org/zimv/websocket-heartbeat-js.svg?branch=master)](https://travis-ci.org/zimv/websocket-heartbeat-js) | ||
| pongTimeout | false | number | 10000 | After the Ping message is sent, the connection will be disconnected without receiving the backend message within 10 seconds | | ||
| reconnectTimeout | false | number | 2000 | Try to reconnect intervals | | ||
| reconnectTimeout | false | number | 2000 | The interval of reconnection | | ||
| pingMsg | false | string | "heartbeat" | Ping message value | | ||
| repeatLimit | false | number | null | The trial times of reconnection。default: unlimited | | ||
const options = { | ||
@@ -88,0 +88,0 @@ url: 'ws://xxxx', |
114
16717
1990
6