reconnecting-websocket
Advanced tools
Comparing version 1.1.0 to 1.2.0
"use strict"; | ||
var DEFAULT_OPTIONS = { | ||
var getDefaultOptions = function () { return ({ | ||
constructor: (typeof WebSocket === 'function') ? WebSocket : null, | ||
@@ -10,3 +10,3 @@ maxReconnectionDelay: 10000, | ||
debug: false, | ||
}; | ||
}); }; | ||
var bypassProperty = function (src, dst, name) { | ||
@@ -29,3 +29,4 @@ Object.defineProperty(dst, name, { | ||
}; | ||
var reassignEventListeners = function (ws, listeners) { | ||
var LEVEL_0_EVENTS = ['onopen', 'onclose', 'onmessage', 'onerror']; | ||
var reassignEventListeners = function (ws, oldWs, listeners) { | ||
Object.keys(listeners).forEach(function (type) { | ||
@@ -37,2 +38,5 @@ listeners[type].forEach(function (_a) { | ||
}); | ||
if (oldWs) { | ||
LEVEL_0_EVENTS.forEach(function (name) { ws[name] = oldWs[name]; }); | ||
} | ||
}; | ||
@@ -53,3 +57,3 @@ var ReconnectingWebsocket = function (url, protocols, options) { | ||
// Set config. Not using `Object.assign` because of IE11 | ||
var config = DEFAULT_OPTIONS; | ||
var config = getDefaultOptions(); | ||
Object.keys(config) | ||
@@ -87,2 +91,3 @@ .filter(function (key) { return options.hasOwnProperty(key); }) | ||
log('connect'); | ||
var oldWs = ws; | ||
ws = new config.constructor(url, protocols); | ||
@@ -127,3 +132,3 @@ connectingTimeout = setTimeout(function () { | ||
}); | ||
reassignEventListeners(ws, listeners); | ||
reassignEventListeners(ws, oldWs, listeners); | ||
}; | ||
@@ -130,0 +135,0 @@ log('init'); |
17
index.ts
@@ -1,2 +0,2 @@ | ||
const DEFAULT_OPTIONS = { | ||
const getDefaultOptions = () => ({ | ||
constructor: (typeof WebSocket === 'function') ? WebSocket : null, | ||
@@ -9,3 +9,3 @@ maxReconnectionDelay: 10000, | ||
debug: false, | ||
}; | ||
}); | ||
@@ -31,3 +31,5 @@ const bypassProperty = (src, dst, name: string) => { | ||
const reassignEventListeners = (ws, listeners) => { | ||
const LEVEL_0_EVENTS = ['onopen', 'onclose', 'onmessage', 'onerror']; | ||
const reassignEventListeners = (ws, oldWs, listeners) => { | ||
Object.keys(listeners).forEach(type => { | ||
@@ -38,2 +40,5 @@ listeners[type].forEach(([listener, options]) => { | ||
}); | ||
if (oldWs) { | ||
LEVEL_0_EVENTS.forEach(name => {ws[name] = oldWs[name]}); | ||
} | ||
}; | ||
@@ -59,3 +64,3 @@ | ||
// Set config. Not using `Object.assign` because of IE11 | ||
const config = DEFAULT_OPTIONS; | ||
const config = getDefaultOptions(); | ||
Object.keys(config) | ||
@@ -88,3 +93,3 @@ .filter(key => options.hasOwnProperty(key)) | ||
log('connect'); | ||
const oldWs = ws; | ||
ws = new (<any>config.constructor)(url, protocols); | ||
@@ -134,3 +139,3 @@ | ||
reassignEventListeners(ws, listeners); | ||
reassignEventListeners(ws, oldWs, listeners); | ||
}; | ||
@@ -137,0 +142,0 @@ |
{ | ||
"name": "reconnecting-websocket", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Reconnecting WebSocket", | ||
@@ -30,3 +30,2 @@ "main": "build/index.js", | ||
"opn-cli": "^3.1.0", | ||
"sinon": "^1.17.4", | ||
"ws": "^1.1.1" | ||
@@ -33,0 +32,0 @@ }, |
@@ -5,10 +5,6 @@ # Reconnecting WebSocket | ||
# WORK IN PROGRESS | ||
Please do not use for production :( | ||
## Features | ||
- Small (~150 LOC) | ||
- WebSocket API compatible (same interface DOM1, DOM2 event model) - **WIP** | ||
- WebSocket API compatible (same interface, Level0 and Level2 event model) | ||
- Fully configurable | ||
@@ -20,3 +16,3 @@ - Multiplatform (Web, ServiceWorkers, Node.js, React Native) | ||
- Handle connection timeouts | ||
- Full test coverage - **WIP** | ||
- Full test coverage | ||
- Debug mode | ||
@@ -30,2 +26,18 @@ | ||
## Run tests | ||
```bash | ||
# clone | ||
git clone https://github.com/pladaria/reconnecting-websocket | ||
# enter | ||
cd reconnecting-websocket | ||
# install deps | ||
npm install | ||
# run tests | ||
npm test | ||
# review the test coverage report | ||
npm run report | ||
``` | ||
## Usage | ||
@@ -57,3 +69,3 @@ | ||
```javascript | ||
const DEFAULT_OPTIONS = { | ||
const defaultOptions = { | ||
constructor: (typeof WebSocket === 'function') ? WebSocket : null, | ||
@@ -92,4 +104,21 @@ maxReconnectionDelay: 10000, | ||
#### Max retries | ||
When the max retries limit is reached, an error event with code `EHOSTDOWN` is emitted. | ||
By default, `maxRetries` is set to `Infinity`. | ||
```javascript | ||
const WebSocket = require('reconnecting-websocket'); | ||
const ws = new WebSocket('ws://my.site.com', null, {maxRetries: 3}); | ||
ws.onerror = (err) => { | ||
if (err.code === 'EHOSTDOWN') { | ||
console.log('server down'); | ||
} | ||
}; | ||
``` | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5
727
120
0
68405