Skip to content

Commit 954d9d6

Browse files
feat: Enhance WebSocketConnector with reconnection logic and configuration options
- Added `_currentAttempt` property to track reconnection attempts. - Introduced `_handleConnectionFailure` method to manage reconnection logic with exponential backoff. - Updated `WebSocketOptions` to replace `idleTimeout` with `idleTimeoutMs` and `reconnectOnFailure` with `reconnectAttempts`. - Modified `WebSocketConnectorBase` to utilize new options and handle connection failures. - Updated tests to reflect changes in configuration options and validate reconnection behavior. - Ensured reconnection attempts are capped at a maximum of 10 to prevent excessive retries.
1 parent fbf0c8e commit 954d9d6

20 files changed

+365
-228
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ import { NodeWebSocketConnector } from '@tsdotnet/websocket-connector/node';
5858
const connector = new NodeWebSocketConnector('ws://localhost:8080', {
5959
headers: { 'Authorization': 'Bearer token123' },
6060
protocols: ['v1', 'v2'],
61-
idleTimeout: 30000, // 30 seconds
62-
reconnectOnFailure: true
61+
idleTimeoutMs: 30000, // 30 seconds
62+
reconnectAttempts: 3 // Try to reconnect up to 3 times on failure
6363
});
6464

6565
const connection = await connector.connect();
@@ -162,11 +162,11 @@ interface WebSocketOptions {
162162
/** Custom headers (Node.js only) */
163163
headers?: Record<string, string>;
164164

165-
/** Idle timeout in milliseconds (default: 5000ms) */
166-
idleTimeout?: number;
165+
/** Idle timeout in milliseconds (default: 10000ms) */
166+
idleTimeoutMs?: number;
167167

168-
/** Auto-reconnect on connection failure (default: false) */
169-
reconnectOnFailure?: boolean;
168+
/** Number of reconnection attempts on failure (default: 0) */
169+
reconnectAttempts?: number;
170170
}
171171

172172
const connector = new NodeWebSocketConnector('ws://localhost:8080', {
@@ -175,18 +175,18 @@ const connector = new NodeWebSocketConnector('ws://localhost:8080', {
175175
'Authorization': 'Bearer your-token',
176176
'User-Agent': 'MyApp/1.0'
177177
},
178-
idleTimeout: 60000, // 1 minute
179-
reconnectOnFailure: true
178+
idleTimeoutMs: 60000, // 1 minute
179+
reconnectAttempts: 5 // Try up to 5 times with exponential backoff
180180
});
181181
```
182182

183183
## 🔄 Reconnection Behavior
184184

185-
When `reconnectOnFailure: true` is enabled:
185+
When `reconnectAttempts` is set to a value greater than 0:
186186

187187
```typescript
188188
const connector = new BrowserWebSocketConnector('wss://api.example.com/ws', {
189-
reconnectOnFailure: true
189+
reconnectAttempts: 3 // Try up to 3 times with exponential backoff
190190
});
191191

192192
const connection = await connector.connect();
@@ -207,12 +207,19 @@ connection.subscribe(message => {
207207
});
208208
```
209209

210+
**Reconnection Strategy:**
211+
- **Exponential Backoff**: 1s, 2s, 4s, 8s, 16s, max 30s
212+
- **Maximum Attempts**: Capped at 10 attempts to prevent abuse
213+
- **Virtual Connection Preservation**: All virtual connections remain alive during reconnection attempts
214+
- **Automatic State Management**: Transitions between `Connected``Reconnecting``Connected` or `Disconnected`
215+
- **Configurable Attempts**: Set `reconnectAttempts: 0` to disable reconnection entirely
216+
210217
## 🏗️ Advanced Usage
211218

212219
### Message Filtering and Routing
213220

214221
```typescript
215-
import { filter, map } from 'rxjs/operators';
222+
import { filter, map } from 'rxjs';
216223

217224
const connector = new BrowserWebSocketConnector('wss://api.example.com/ws');
218225
const connection = await connector.connect();
@@ -368,7 +375,7 @@ describe('Advanced WebSocket testing', () => {
368375
describe('Reconnection testing', () => {
369376
it('should maintain virtual connections during reconnection', async () => {
370377
const mockConnector = new MockWebSocketConnector('ws://test', {
371-
reconnectOnFailure: true
378+
reconnectAttempts: 3
372379
});
373380

374381
const connection1 = await mockConnector.connect();

dist/cjs/BrowserWebSocketConnector.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/BrowserWebSocketConnector.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/NodeWebSocketConnector.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)