websocket-heartbeat-js
Advanced tools
Comparing version 1.0.13 to 1.1.0
@@ -1,1 +0,1 @@ | ||
!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]}}(this,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()},"undefined"!=typeof window&&(window.WebsocketHeartbeatJs=n),e.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]}}(this,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.protocols,n=void 0===o?"":o,i=t.pingTimeout,r=void 0===i?15e3:i,s=t.pongTimeout,c=void 0===s?1e4:s,u=t.reconnectTimeout,p=void 0===u?2e3:u,f=t.pingMsg,a=void 0===f?"heartbeat":f,h=t.repeatLimit,l=void 0===h?null:h;this.opts={url:e,protocols:n,pingTimeout:r,pongTimeout:c,reconnectTimeout:p,pingMsg:a,repeatLimit:l},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.opts.protocols?this.ws=new WebSocket(this.opts.url,this.opts.protocols):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()},"undefined"!=typeof window&&(window.WebsocketHeartbeatJs=n),e.default=n}])}); |
@@ -5,6 +5,7 @@ declare module 'websocket-heartbeat-js' { | ||
url: string | ||
protocols: string | string[] | ||
pingTimeout: number | ||
pongTimeout: number | ||
reconnectTimeout: number | ||
pingMsg: string | ||
pingMsg: any | ||
repeatLimit: null | number | ||
@@ -22,6 +23,7 @@ } | ||
url: string | ||
protocols?: string | string[] | ||
pingTimeout?: number | ||
pongTimeout?: number | ||
reconnectTimeout?: number | ||
pingMsg?: string | ||
pingMsg?: any | ||
repeatLimit?: number | null | ||
@@ -28,0 +30,0 @@ }) |
@@ -16,3 +16,4 @@ /** | ||
function WebsocketHeartbeatJs({ | ||
url, | ||
url, | ||
protocols = '', | ||
pingTimeout = 15000, | ||
@@ -22,6 +23,7 @@ pongTimeout = 10000, | ||
pingMsg = 'heartbeat', | ||
repeatLimit = null | ||
repeatLimit = null, | ||
}){ | ||
this.opts ={ | ||
url: url, | ||
protocols, | ||
pingTimeout: pingTimeout, | ||
@@ -47,3 +49,4 @@ pongTimeout: pongTimeout, | ||
try { | ||
this.ws = new WebSocket(this.opts.url); | ||
if(this.opts.protocols) this.ws = new WebSocket(this.opts.url, this.opts.protocols); | ||
else this.ws = new WebSocket(this.opts.url); | ||
this.initEventHandle(); | ||
@@ -50,0 +53,0 @@ } catch (e) { |
{ | ||
"name": "websocket-heartbeat-js", | ||
"version": "1.0.13", | ||
"version": "1.1.0", | ||
"description": "websocket heartbeat", | ||
@@ -8,3 +8,3 @@ "main": "./dist/index.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha-phantomjs -p ./node_modules/.bin/phantomjs /test/unit/index.html", | ||
"test": "node test/unit/server.js", | ||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", | ||
@@ -45,7 +45,7 @@ "build": "node_modules/webpack/bin/webpack.js", | ||
"mocha": "^5.2.0", | ||
"mocha-phantomjs": "^4.1.0", | ||
"phantomjs-prebuilt": "^2.1.16", | ||
"opn": "^6.0.0", | ||
"pre-commit": "^1.2.2", | ||
"webpack": "^4.20.2", | ||
"webpack-cli": "^3.1.2" | ||
"webpack-cli": "^3.1.2", | ||
"ws": "^8.5.0" | ||
}, | ||
@@ -52,0 +52,0 @@ "babel": { |
@@ -1,2 +0,1 @@ | ||
[![Build Status](https://travis-ci.org/zimv/websocket-heartbeat-js.svg?branch=master)](https://travis-ci.org/zimv/websocket-heartbeat-js) | ||
<a href="https://www.npmjs.com/package/websocket-heartbeat-js" alt="NPM latest version"><img src="https://img.shields.io/npm/v/websocket-heartbeat-js.svg"></a> | ||
@@ -80,6 +79,7 @@ <a href="https://npms.io/search?q=websocket-heartbeat-js" alt="NPM latest version"><img src="https://badges.npms.io/websocket-heartbeat-js.svg"></a> | ||
| url | true | string | none | websocket service address | | ||
| protocols | false | string or string[] | none | new WebSocket(, protocols)| | ||
| pingTimeout | false | number | 15000 | A heartbeat is sent every 15 seconds. If any backend message is received, the timer will reset | | ||
| 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 | The interval of reconnection | | ||
| pingMsg | false | string | "heartbeat" | Ping message value | | ||
| pingMsg | false | any | "heartbeat" | Ping message value | | ||
| repeatLimit | false | number | null | The trial times of reconnection。default: unlimited | | ||
@@ -86,0 +86,0 @@ |
155
17127
6