diff --git a/README.md b/README.md index 3b1e6be..06e91a1 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,7 @@ import WSReqonet from "ws-reqonet"; const url = `ws://localhost:3001/`; const protocols = []; const options = { - maxReconnectAttempts: 5, - maxRetryAttempts: 3, + maxRetryAttempts: 10, queueMessage: true, }; @@ -81,41 +80,34 @@ Either a single protocol string or an array of protocol strings. These strings a Type: `object` -#### maxReconnectAttempts - -Type: `number`\ -Default: 5 - -Number of times it attempts to reconnect within a retry - #### maxRetryAttempts +The maximum number of retries - how many attempts at reconnecting + Type: `number`\ Default: 5 -The maximum number of retries - how many attempts at reconnecting - #### queueMessage +Whether to store 'send' data when the connection is broken, which is to be relayed when connection is restored. + Type: `boolean`\ Default: true -Whether to store 'send' data when the connection is broken, which is to be relayed when connection is restored. - #### disableReconnect +Whether to disable reconnection + Type: `boolean`\ Default: false -Whether to disable reconnection - #### debug +Whether to run in debug mode which enables logging to dev tools + Type: `boolean`\ Default: false -Whether to run in debug mode which enables logging to dev tools - ### events #### open, close, message, error diff --git a/index.ts b/index.ts index bdc8341..fd4052a 100644 --- a/index.ts +++ b/index.ts @@ -1,10 +1,9 @@ import { EventEmitter } from "events"; +const MAX_RECONNECT_ATTEMPTS = 10; const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export interface WSReqonetOptions { - /** # of times to reconnect within a retry */ - maxReconnectAttempts?: number; - /** # of attempts at reconnecting */ + /** # attempts to reconnect */ maxRetryAttempts?: number; /** Whether to store 'send' data when connection is broken */ queueMessage?: boolean; @@ -16,14 +15,13 @@ export interface WSReqonetOptions { // websocket with reconnection on exponential back-off export default class WSReqonet extends EventEmitter { private ws: WebSocket; - private reconnectAttempts = 0; - private maxReconnectAttempts: number; private retryAttempts = 0; private maxRetryAttempts: number; private queueMessage: boolean; private messageQueue: Array = []; + private reconnectAttempts = 0; private intervalRef = 0; private forcedClose = false; private disableReconnect: boolean; @@ -35,8 +33,7 @@ export default class WSReqonet extends EventEmitter { ) { super(); - this.maxReconnectAttempts = options.maxReconnectAttempts ?? 5; - this.maxRetryAttempts = options.maxRetryAttempts ?? 5; + this.maxRetryAttempts = options.maxRetryAttempts ?? 10; this.queueMessage = options.queueMessage ?? true; this.disableReconnect = options.disableReconnect ?? false; @@ -106,7 +103,7 @@ export default class WSReqonet extends EventEmitter { const TIMEOUT = Math.pow(2, this.retryAttempts + 1) * 1000; const reconnectHandler = () => { - if (this.reconnectAttempts < this.maxReconnectAttempts) { + if (this.reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { this.reconnectAttempts++; console.log("ws: reconnecting - attempt: ", this.reconnectAttempts);