@yz-social/webrtc 0.1.12 → 0.1.14
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/index.js +11 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -46,9 +46,8 @@ export class WebRTC {
|
|
|
46
46
|
if (!pc) return null;
|
|
47
47
|
const state = pc.connectionState;
|
|
48
48
|
if (state === 'connected') return this.signalsReadyResolver?.();
|
|
49
|
-
if (['new', 'connecting'].includes(state)) return null;
|
|
50
|
-
// closed, disconnected
|
|
51
|
-
this.log('connectionstatechange signaling/connection:', pc.signalingState, state);
|
|
49
|
+
if (['new', 'connecting'/*, 'failed'*/].includes(state)) return null; // On failed, iceconnectionstatechange will restartIce.
|
|
50
|
+
// closed, disconnected: resolve this.closed promise.
|
|
52
51
|
this.cleanup();
|
|
53
52
|
return resolve(pc);
|
|
54
53
|
};
|
|
@@ -67,7 +66,7 @@ export class WebRTC {
|
|
|
67
66
|
this.pc.ondatachannel = e => this.ondatachannel(e.channel);
|
|
68
67
|
this.pc.onicecandidateerror = error => {
|
|
69
68
|
const {errorCode, errorText, url, address, port} = error;
|
|
70
|
-
this.
|
|
69
|
+
this.log(`${errorCode}: ${errorText || '(ice failure)'} ${address}:${port} @${url}`);
|
|
71
70
|
};
|
|
72
71
|
this.pc.oniceconnectionstatechange = () => {
|
|
73
72
|
if (!this.pc) return;
|
|
@@ -75,7 +74,7 @@ export class WebRTC {
|
|
|
75
74
|
case 'completed':
|
|
76
75
|
this._resolveIceCompleted?.(true);
|
|
77
76
|
break;
|
|
78
|
-
case 'failed':
|
|
77
|
+
case 'failed': // This doesn't mean the connection is broken. It could just be looking for a better transport.
|
|
79
78
|
this.pc.restartIce();
|
|
80
79
|
break;
|
|
81
80
|
default:
|
|
@@ -203,13 +202,14 @@ export class WebRTC {
|
|
|
203
202
|
pendingSignals = [];
|
|
204
203
|
responseSerializer = Promise.resolve();
|
|
205
204
|
async respond(signals = []) { // Apply a list of signals, and promise a list of responding signals as soon as any are available, or empty list when connected
|
|
206
|
-
// This is used by a peer that is receiving signals in an out-of-band network request, and
|
|
205
|
+
// This is used by a peer that is receiving signals in an out-of-band network request, and waiting for a response. (Compare transferSignals.)
|
|
207
206
|
return this.responseSerializer = this.responseSerializer.then(async () => {
|
|
208
207
|
this.log('respond', signals.length, 'signals');
|
|
209
|
-
const {promise, resolve} = Promise.withResolvers;
|
|
208
|
+
const {promise, resolve} = Promise.withResolvers(); // FIXME: this breaks Firefox.
|
|
209
|
+
//const {promise, resolve} = {}; // FIXME: tests "pass" on Firefox with this version.
|
|
210
210
|
this.signalsReadyResolver = resolve;
|
|
211
211
|
await this.onSignals(signals);
|
|
212
|
-
await promise;
|
|
212
|
+
if (!this.pendingSignals.length) await promise;
|
|
213
213
|
this.signalsReadyResolver = null;
|
|
214
214
|
return this.collectPendingSignals();
|
|
215
215
|
});
|
|
@@ -330,11 +330,12 @@ export class WebRTC {
|
|
|
330
330
|
return;
|
|
331
331
|
}
|
|
332
332
|
const remote = stats.get(candidatePair.remoteCandidateId);
|
|
333
|
-
const {protocol, candidateType, address} = remote;
|
|
333
|
+
const {protocol, candidateType, address, port} = remote;
|
|
334
334
|
const now = Date.now();
|
|
335
335
|
const statsElapsed = now - this.connectionStartTime;
|
|
336
|
+
const local = stats.get(candidatePair.localCandidateId);
|
|
336
337
|
Object.assign(this, {stats, transport, candidatePair, remote, protocol, candidateType, statsTime: now, statsElapsed});
|
|
337
|
-
if (doLogging) console.info(this.name,
|
|
338
|
+
if (doLogging) console.info(new Date(), this.name, this.pc?.connectionState, protocol, candidateType, `${local.address}:${local.port}=>${address}:${port}`, (statsElapsed/1e3).toFixed(1));
|
|
338
339
|
}
|
|
339
340
|
|
|
340
341
|
static getPublicIP(stunServer = "stun:stun.l.google.com:19302") { // Promise external/WAN/public IP addresses for this device.
|