WebRTC use data channel from server.

This commit is contained in:
Miroslav Šedivý 2021-02-12 23:46:25 +01:00
parent ed495f2fd9
commit fbec6cee63

View File

@ -119,15 +119,9 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
this._peer.ontrack = this.onTrack.bind(this) this._peer.ontrack = this.onTrack.bind(this)
this._peer.ondatachannel = this.onDataChannel.bind(this)
this._peer.addTransceiver('audio', { direction: 'recvonly' }) this._peer.addTransceiver('audio', { direction: 'recvonly' })
this._peer.addTransceiver('video', { 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 }) this._peer.setRemoteDescription({ type: 'offer', sdp })
if (this.candidates.length > 0) { if (this.candidates.length > 0) {
@ -225,6 +219,16 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
this.emit('track', event) 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() { private onConnected() {
if (!this.connected) { if (!this.connected) {
this._log.warn(`onConnected called while being disconnected`) this._log.warn(`onConnected called while being disconnected`)