Merge pull request #220 from feross/handle-errors

Handle peer 'error' events correctly
This commit is contained in:
Feross Aboukhadijeh 2017-04-07 17:35:43 -07:00 committed by GitHub
commit 291df676fd

View File

@ -276,7 +276,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
var peer
if (data.offer && data.peer_id) {
debug('creating peer (from remote offer)')
peer = new Peer({
peer = self._createPeer({
trickle: false,
config: self.client._rtcConfig,
wrtc: self.client._wrtc
@ -389,7 +389,7 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
function generateOffer () {
var offerId = randombytes(20).toString('hex')
debug('creating peer (from _generateOffers)')
var peer = self.peers[offerId] = new Peer({
var peer = self.peers[offerId] = self._createPeer({
initiator: true,
trickle: false,
config: self.client._rtcConfig,
@ -419,4 +419,28 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
}
}
WebSocketTracker.prototype._createPeer = function (opts) {
var self = this
var peer = new Peer(opts)
peer.once('error', onError)
peer.once('connect', onConnect)
return peer
// Handle peer 'error' events that are fired *before* the peer is emitted in
// a 'peer' event.
function onError (err) {
self.client.emit('warning', new Error('Connection error: ' + err.message))
peer.destroy()
}
// Once the peer is emitted in a 'peer' event, then it's the consumer's
// responsibility to listen for errors, so the listeners are removed here.
function onConnect () {
peer.removeListener('error', onError)
peer.removeListener('connect', onConnect)
}
}
function noop () {}