From fbec6cee63263fcc72d819beffca6c0d32eebb9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 12 Feb 2021 23:46:25 +0100 Subject: [PATCH] WebRTC use data channel from server. --- src/component/internal/webrtc.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/component/internal/webrtc.ts b/src/component/internal/webrtc.ts index bfd0a51c..a99242a5 100644 --- a/src/component/internal/webrtc.ts +++ b/src/component/internal/webrtc.ts @@ -119,15 +119,9 @@ export class NekoWebRTC extends EventEmitter { } this._peer.ontrack = this.onTrack.bind(this) + this._peer.ondatachannel = this.onDataChannel.bind(this) this._peer.addTransceiver('audio', { direction: 'recvonly' }) this._peer.addTransceiver('video', { direction: 'recvonly' }) - - this._channel = this._peer.createDataChannel('data') - this._channel.onerror = this.onDisconnected.bind(this, new Error('peer data channel error')) - this._channel.onmessage = this.onData.bind(this) - this._channel.onopen = this.onConnected.bind(this) - this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed')) - this._peer.setRemoteDescription({ type: 'offer', sdp }) if (this.candidates.length > 0) { @@ -225,6 +219,16 @@ export class NekoWebRTC extends EventEmitter { this.emit('track', event) } + private onDataChannel(event: RTCDataChannelEvent) { + this._log.debug(`received data channel from peer: ${event.channel.label}`, event) + + this._channel = event.channel + this._channel.onerror = this.onDisconnected.bind(this, new Error('peer data channel error')) + this._channel.onmessage = this.onData.bind(this) + this._channel.onopen = this.onConnected.bind(this) + this._channel.onclose = this.onDisconnected.bind(this, new Error('peer data channel closed')) + } + private onConnected() { if (!this.connected) { this._log.warn(`onConnected called while being disconnected`)