diff --git a/example/legacy.html b/example/legacy.html new file mode 100644 index 0000000..28bbd94 --- /dev/null +++ b/example/legacy.html @@ -0,0 +1,137 @@ + + + + + + + Legacy Two-Browser Test + + + +

Legacy Two-Browser Test

+

+ Open this page in two browsers/tabs. In browser A click Create Lobby. + Copy the lobby code and paste it in browser B, then click Join Lobby. +

+ +
+ + + + +
+ +
+ Your peer id: (not ready)
+ Current lobby: (none) +
+ + + + + + + + diff --git a/lib/peer.ts b/lib/peer.ts index a91c5a2..e5f7590 100644 --- a/lib/peer.ts +++ b/lib/peer.ts @@ -59,36 +59,7 @@ export default class Peer { } this.conn.addEventListener('negotiationneeded', () => { this.politenessTimeout = setTimeout(() => { - (async () => { - try { - if (this.closing) { - return - } - this.makingOffer = true - if (process.env.NODE_ENV === 'test') { - // Running tests with node and the wrtc package causes the - // setLocalDescription to fail with undefined as argment. - await this.conn.setLocalDescription(await this.conn.createOffer()) - } else { - await this.conn.setLocalDescription() - } - const description = this.conn.localDescription - if (description != null) { - await this.testSessionWrapper?.(description, this.config, this.network.id, this.id) - this.signaling.send({ - type: 'description', - source: this.network.id, - recipient: this.id, - description - }) - } - } catch (e) { - const error = new SignalingError('unknown-error', e as string) - this.network._onSignalingError(error) - } finally { - this.makingOffer = false - } - })().catch(_ => {}) + void this.handleNegotiationNeeded() }, this.polite ? 100 : 0) }) @@ -220,6 +191,37 @@ export default class Peer { } } + private async handleNegotiationNeeded (): Promise { + try { + if (this.closing) { + return + } + this.makingOffer = true + if (process.env.NODE_ENV === 'test') { + // Running tests with node and the wrtc package causes the + // setLocalDescription to fail with undefined as argment. + await this.conn.setLocalDescription(await this.conn.createOffer()) + } else { + await this.conn.setLocalDescription() + } + const description = this.conn.localDescription + if (description != null) { + await this.testSessionWrapper?.(description, this.config, this.network.id, this.id) + this.signaling.send({ + type: 'description', + source: this.network.id, + recipient: this.id, + description + }) + } + } catch (e) { + const error = new SignalingError('unknown-error', e as string) + this.network._onSignalingError(error) + } finally { + this.makingOffer = false + } + } + private onError (e: Event): void { this.network.emit('rtcerror', e) if (this.network.listenerCount('rtcerror') === 0) {