mappum / electron-webrtc

⚛ Use WebRTC in Node.js via a hidden Electron process
317 stars 47 forks source link

RTCDataChannel.send(): emit('error') #124

Open brubbel opened 7 years ago

brubbel commented 7 years ago

Hi,

On very rare occasions (every few days to weeks), RTCDataChannel.send() emits an error like this below, after which electron-webrtc becomes unresponsive:

{ Error: Error evaluating "
        var pc = conns["j"]
        var dc = pc.dataChannels[1]

        if (dc.readyState === 'open') {
          var data = "whatever-data-here"
          dc.send(data)
        }
        dc.bufferedAmount
      " in "window": Failed to execute 'send' on 'RTCDataChannel': Could not send data
    at Daemon.<anonymous> (/opt/pysern/peerjs/node_modules/electron-eval/lib/index.js:81:21)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at Daemon.emit (events.js:210:7)
    at ChildProcess.<anonymous> (/opt/pysern/peerjs/node_modules/electron-eval/lib/index.js:195:27)
    at emitOne (events.js:115:13)
    at ChildProcess.emit (events.js:210:7)
    at DestroyableTransform.<anonymous> (/opt/pysern/peerjs/node_modules/electron-eval/lib/index.js:228:29)
    at emitOne (events.js:115:13)
    at DestroyableTransform.emit (events.js:210:7)
  original: 'Failed to execute \'send\' on \'RTCDataChannel\': Could not send data' }

This question is not about why the error is generated, but how to catch this event. Reading the docs of electron-eval it says:

If callback is not provided and the eval causes an error, the daemon will emit an error event.

A callback is however provided here, but the signal is emitted by the RTCDataChannel object. The only place I found to subscribe to this event is in RTCPeerConnection.js by adding the following to prevent that the rtc data-connection becomes unresponsive:

event.channel.on('error', function(err) {
    event.channel.close();
});

The question:

  1. Is this intended behaviour (who should emit the error)?
  2. If so, what is the correct location to handle the error, since a try/catch block around RTCDataChannel.send() does not work, and neither the daemon or wrtc emit a signal.

Thanks!