Skip to content

Commit

Permalink
feat: add options for websocket (#3)
Browse files Browse the repository at this point in the history
Co-authored-by: Zerounary <[email protected]>
  • Loading branch information
bytemain and Zerounary authored May 13, 2024
1 parent 05a2f7c commit 87833b4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const rws = new ReconnectingWebSocket('ws://my.site.com', [], options);
```typescript
type Options = {
WebSocket?: any; // WebSocket constructor, if none provided, defaults to global WebSocket
WebSocketOptions?: any; // WebSocket options
maxReconnectionDelay?: number; // max delay in ms between reconnections
minReconnectionDelay?: number; // min delay in ms between reconnections
reconnectionDelayGrowFactor?: number; // how fast the reconnection delay grows
Expand All @@ -114,6 +115,7 @@ type Options = {

```javascript
WebSocket: undefined,
WebSocketOptions: {},
maxReconnectionDelay: 10000,
minReconnectionDelay: 1000 + Math.random() * 4000,
reconnectionDelayGrowFactor: 1.3,
Expand Down
7 changes: 4 additions & 3 deletions reconnecting-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type CloseEvent = Events.CloseEvent;

export type Options = {
WebSocket?: any;
WebSocketOptions?: any | {};
maxReconnectionDelay?: number;
minReconnectionDelay?: number;
reconnectionDelayGrowFactor?: number;
Expand Down Expand Up @@ -378,10 +379,10 @@ export default class ReconnectingWebSocket {
if (this._closeCalled) {
return;
}
this._debug('connect', {url, protocols: this._protocols});
this._debug('connect', {url, protocols: this._protocols, options: this._options});
this._ws = this._protocols
? new WebSocket(url, this._protocols)
: new WebSocket(url);
? new WebSocket(url, this._protocols, this._options.WebSocketOptions)
: new WebSocket(url, this._options.WebSocketOptions);
this._ws!.binaryType = this._binaryType;
this._connectLock = false;
this._addListeners();
Expand Down

0 comments on commit 87833b4

Please sign in to comment.