@marianmeres/webrtc 1.0.3 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +35 -0
- package/README.md +32 -0
- package/dist/types.d.ts +14 -0
- package/dist/webrtc-manager.js +18 -0
- package/package.json +1 -1
package/API.md
CHANGED
|
@@ -759,6 +759,13 @@ interface WebRtcManagerConfig {
|
|
|
759
759
|
/** Initial reconnection delay in ms (doubles each attempt). Default: 1000 */
|
|
760
760
|
reconnectDelay?: number;
|
|
761
761
|
|
|
762
|
+
/** Callback to control whether reconnection should proceed */
|
|
763
|
+
shouldReconnect?: (context: {
|
|
764
|
+
attempt: number;
|
|
765
|
+
maxAttempts: number;
|
|
766
|
+
strategy: "ice-restart" | "full";
|
|
767
|
+
}) => boolean;
|
|
768
|
+
|
|
762
769
|
/** Enable debug logging. Default: false */
|
|
763
770
|
debug?: boolean;
|
|
764
771
|
|
|
@@ -947,3 +954,31 @@ manager.on('reconnecting', ({ attempt, strategy }) => {
|
|
|
947
954
|
}
|
|
948
955
|
});
|
|
949
956
|
```
|
|
957
|
+
|
|
958
|
+
### Conditional Reconnection
|
|
959
|
+
|
|
960
|
+
Use the `shouldReconnect` callback to suppress reconnection when the peer disconnected intentionally:
|
|
961
|
+
|
|
962
|
+
```typescript
|
|
963
|
+
let peerLeftIntentionally = false;
|
|
964
|
+
|
|
965
|
+
const manager = new WebRtcManager(factory, {
|
|
966
|
+
autoReconnect: true,
|
|
967
|
+
shouldReconnect: ({ attempt, maxAttempts, strategy }) => {
|
|
968
|
+
// Return false to suppress reconnection
|
|
969
|
+
return !peerLeftIntentionally;
|
|
970
|
+
},
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
// Track intentional disconnects via data channel
|
|
974
|
+
manager.on('data_channel_message', ({ data }) => {
|
|
975
|
+
if (JSON.parse(data).type === 'bye') {
|
|
976
|
+
peerLeftIntentionally = true;
|
|
977
|
+
}
|
|
978
|
+
});
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
The callback receives:
|
|
982
|
+
- `attempt`: Current attempt number (1-based)
|
|
983
|
+
- `maxAttempts`: Configured maximum attempts
|
|
984
|
+
- `strategy`: `"ice-restart"` or `"full"`
|
package/README.md
CHANGED
|
@@ -51,6 +51,7 @@ const manager = new WebRtcManager(factory, config);
|
|
|
51
51
|
- `autoReconnect`: Enable automatic reconnection (default: false)
|
|
52
52
|
- `maxReconnectAttempts`: Max reconnection attempts (default: 5)
|
|
53
53
|
- `reconnectDelay`: Initial reconnection delay in ms (default: 1000)
|
|
54
|
+
- `shouldReconnect`: Callback to control whether reconnection should proceed (see below)
|
|
54
55
|
- `debug`: Enable debug logging (default: false)
|
|
55
56
|
- `logger`: Custom logger instance implementing `Logger` interface (default: console)
|
|
56
57
|
|
|
@@ -129,6 +130,37 @@ const unsub = manager.subscribe((state) => {
|
|
|
129
130
|
- `EVENT_MICROPHONE_FAILED`
|
|
130
131
|
- `EVENT_ERROR`
|
|
131
132
|
|
|
133
|
+
### Controlling Reconnection
|
|
134
|
+
|
|
135
|
+
When `autoReconnect` is enabled, you can use the `shouldReconnect` callback to conditionally suppress reconnection attempts. This is useful when the remote peer disconnected intentionally (e.g., left the call) rather than due to network failure.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
let peerLeftIntentionally = false;
|
|
139
|
+
|
|
140
|
+
const manager = new WebRtcManager(factory, {
|
|
141
|
+
autoReconnect: true,
|
|
142
|
+
shouldReconnect: ({ attempt, maxAttempts, strategy }) => {
|
|
143
|
+
if (peerLeftIntentionally) {
|
|
144
|
+
return false; // Don't reconnect
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Listen for "goodbye" message from peer before they disconnect
|
|
151
|
+
manager.on(WebRtcManager.EVENT_DATA_CHANNEL_MESSAGE, ({ data }) => {
|
|
152
|
+
const msg = JSON.parse(data);
|
|
153
|
+
if (msg.type === 'bye') {
|
|
154
|
+
peerLeftIntentionally = true;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The callback receives:
|
|
160
|
+
- `attempt`: Current reconnection attempt (1-based)
|
|
161
|
+
- `maxAttempts`: Configured maximum attempts
|
|
162
|
+
- `strategy`: `"ice-restart"` (attempts 1-2) or `"full"` (attempts 3+)
|
|
163
|
+
|
|
132
164
|
## Examples
|
|
133
165
|
|
|
134
166
|
### Basic Usage (Vanilla JavaScript)
|
package/dist/types.d.ts
CHANGED
|
@@ -26,6 +26,20 @@ export interface WebRtcManagerConfig {
|
|
|
26
26
|
maxReconnectAttempts?: number;
|
|
27
27
|
/** Initial reconnection delay in ms. Doubles with each attempt. Defaults to 1000. */
|
|
28
28
|
reconnectDelay?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Callback to determine whether reconnection should be attempted.
|
|
31
|
+
* Called before each reconnection attempt when autoReconnect is enabled.
|
|
32
|
+
* Return false to suppress reconnection (e.g., when peer disconnected intentionally).
|
|
33
|
+
* If not provided, reconnection proceeds automatically up to maxReconnectAttempts.
|
|
34
|
+
*/
|
|
35
|
+
shouldReconnect?: (context: {
|
|
36
|
+
/** Current reconnection attempt number (1-based) */
|
|
37
|
+
attempt: number;
|
|
38
|
+
/** Maximum configured reconnection attempts */
|
|
39
|
+
maxAttempts: number;
|
|
40
|
+
/** Strategy that will be used: "ice-restart" for first attempts, "full" for later */
|
|
41
|
+
strategy: "ice-restart" | "full";
|
|
42
|
+
}) => boolean;
|
|
29
43
|
/** Enable debug logging. Defaults to false. */
|
|
30
44
|
debug?: boolean;
|
|
31
45
|
/** Custom logger instance. If not provided, falls back to console. */
|
package/dist/webrtc-manager.js
CHANGED
|
@@ -800,6 +800,21 @@ export class WebRtcManager {
|
|
|
800
800
|
});
|
|
801
801
|
return;
|
|
802
802
|
}
|
|
803
|
+
// Determine strategy for this attempt (next attempt number is current + 1)
|
|
804
|
+
const nextAttempt = this.#reconnectAttempts + 1;
|
|
805
|
+
const strategy = nextAttempt <= 2 ? "ice-restart" : "full";
|
|
806
|
+
// Check shouldReconnect callback if provided
|
|
807
|
+
if (this.#config.shouldReconnect) {
|
|
808
|
+
const shouldProceed = this.#config.shouldReconnect?.({
|
|
809
|
+
attempt: nextAttempt,
|
|
810
|
+
maxAttempts,
|
|
811
|
+
strategy,
|
|
812
|
+
});
|
|
813
|
+
if (!shouldProceed) {
|
|
814
|
+
this.#debug("Reconnection suppressed by shouldReconnect callback");
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
803
818
|
// Transition to RECONNECTING state
|
|
804
819
|
this.#dispatch(WebRtcFsmEvent.RECONNECTING);
|
|
805
820
|
// Attempt reconnection with exponential backoff
|
|
@@ -838,6 +853,9 @@ export class WebRtcManager {
|
|
|
838
853
|
// perform the signaling handshake (create offer/answer exchange) to
|
|
839
854
|
// complete the reconnection.
|
|
840
855
|
try {
|
|
856
|
+
// Clean up old connection and reset to IDLE so connect() creates a new PC
|
|
857
|
+
this.#cleanup();
|
|
858
|
+
this.#dispatch(WebRtcFsmEvent.RESET);
|
|
841
859
|
await this.connect();
|
|
842
860
|
// If successful, onconnectionstatechange will reset attempts
|
|
843
861
|
}
|