reconnecting-websocket
Advanced tools
Comparing version 2.0.0 to 3.0.0
78
index.ts
@@ -0,1 +1,10 @@ | ||
/* | ||
Ready state constants | ||
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Ready_state_constants | ||
CONNECTING 0 The connection is not yet open. | ||
OPEN 1 The connection is open and ready to communicate. | ||
CLOSING 2 The connection is in the process of closing. | ||
CLOSED 3 The connection is closed or couldn't be opened. | ||
*/ | ||
type Options = { | ||
@@ -103,2 +112,22 @@ constructor?: new(url: string, protocols?: string | string[]) => WebSocket; | ||
const handleClose = () => { | ||
log('close'); | ||
retriesCount++; | ||
log('retries count:', retriesCount); | ||
if (retriesCount > config.maxRetries) { | ||
emitError('EHOSTDOWN', 'Too many failed connection attempts'); | ||
return; | ||
} | ||
if (!reconnectDelay) { | ||
reconnectDelay = initReconnectionDelay(config); | ||
} else { | ||
reconnectDelay = updateReconnectionDelay(config, reconnectDelay); | ||
} | ||
log('reconnectDelay:', reconnectDelay); | ||
if (shouldRetry) { | ||
setTimeout(connect, reconnectDelay); | ||
} | ||
}; | ||
const connect = () => { | ||
@@ -131,22 +160,4 @@ log('connect'); | ||
ws.addEventListener('close', () => { | ||
log('close'); | ||
retriesCount++; | ||
log('retries count:', retriesCount); | ||
if (retriesCount > config.maxRetries) { | ||
emitError('EHOSTDOWN', 'Too many failed connection attempts'); | ||
return; | ||
} | ||
if (!reconnectDelay) { | ||
reconnectDelay = initReconnectionDelay(config); | ||
} else { | ||
reconnectDelay = updateReconnectionDelay(config, reconnectDelay); | ||
} | ||
log('reconnectDelay:', reconnectDelay); | ||
ws.addEventListener('close', handleClose); | ||
if (shouldRetry) { | ||
setTimeout(connect, reconnectDelay); | ||
} | ||
}); | ||
reassignEventListeners(ws, oldWs, listeners); | ||
@@ -158,3 +169,3 @@ }; | ||
this.close = (code = 1000, reason = '', {keepClosed = false, delay = 0} = {}) => { | ||
this.close = (code = 1000, reason = '', {keepClosed = false, fastClose = true, delay = 0} = {}) => { | ||
if (delay) { | ||
@@ -164,3 +175,30 @@ reconnectDelay = delay; | ||
shouldRetry = !keepClosed; | ||
ws.close(code, reason); | ||
if (fastClose) { | ||
const fakeCloseEvent = <CloseEvent>{ | ||
code, | ||
reason, | ||
wasClean: true, | ||
}; | ||
// execute close listeners soon with a fake closeEvent | ||
// and remove all close listeners from the WS instance | ||
// so they don't get fired on the real close. | ||
handleClose(); | ||
if (Array.isArray(listeners.close)) { | ||
listeners.close.forEach(([listener, options]) => { | ||
listener(fakeCloseEvent); | ||
ws.removeEventListener('close', listener, options); | ||
}); | ||
} | ||
if (ws.onclose) { | ||
ws.onclose(fakeCloseEvent); | ||
ws.onclose = null; | ||
} | ||
} | ||
}; | ||
@@ -167,0 +205,0 @@ |
{ | ||
"name": "reconnecting-websocket", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Reconnecting WebSocket", | ||
"main": "build/index.js", | ||
"typings": "build/index.d.ts", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"pretest": "npm run build", | ||
"test": "nyc ava --verbose --serial test/test.js", | ||
"pretest": "npm run build", | ||
"prebuild": "rimraf dist", | ||
"report": "nyc report --reporter=html && opn coverage/index.html", | ||
@@ -32,2 +33,3 @@ "prepublish": "npm test" | ||
"opn-cli": "^3.1.0", | ||
"rimraf": "^2.5.4", | ||
"typescript": "1.8.10", | ||
@@ -47,3 +49,6 @@ "ws": "^1.1.1" | ||
}, | ||
"publishConfig": { | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
"homepage": "https://github.com/pladaria/reconnecting-websocket#readme" | ||
} |
@@ -17,2 +17,3 @@ # Reconnecting WebSocket | ||
- Debug mode | ||
- Fast close (new in version 3). | ||
@@ -92,7 +93,8 @@ ## Install | ||
```javascript | ||
close(code = 1000, reason = '', {keepClosed: boolean, delay: number}) | ||
close(code = 1000, reason = '', {keepClosed: boolean, fastClose: boolean, delay: number}) | ||
``` | ||
- Use the `keepClosed` option to keep the WebSocket closed or automatically reconnect (default `false`). | ||
- Use the `delay` options to set the initial delay for the next connection retry (ignored if `0`). | ||
- If `fastClose` option is `true`, all close listeners are executed as soon as the close() method is called, otherwise it waits until the websocket closing protocol finishes, this can be a long time if there's no connection (default `true`). Keep in mind that with this option, it may happen that the close event is fired with a ready state of `CLOSING`. | ||
- Use the `delay` option to set the initial delay for the next connection retry (ignored if `0`). | ||
@@ -99,0 +101,0 @@ #### Using alternative constructor |
@@ -7,3 +7,3 @@ { | ||
"sourceMap": false, | ||
"outDir": "build", | ||
"outDir": "dist", | ||
"declaration": true | ||
@@ -13,4 +13,4 @@ }, | ||
"node_modules", | ||
"build" | ||
"dist" | ||
] | ||
} |
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
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
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
133
21044
7
8
411
1