mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-18 20:21:36 +00:00
Handle peer 'error' events correctly
Handle peer 'error' events that are fired *before* the peer is emitted in a 'peer' event. Once the peer is emitted in a 'peer' event, then it's the consumer's responsibility to listen for errors. This fixes the most common error in WebTorrent Desktop according to our telemetry.
This commit is contained in:
parent
c7a2718290
commit
48d0ea42ad
@ -276,7 +276,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
|
|||||||
var peer
|
var peer
|
||||||
if (data.offer && data.peer_id) {
|
if (data.offer && data.peer_id) {
|
||||||
debug('creating peer (from remote offer)')
|
debug('creating peer (from remote offer)')
|
||||||
peer = new Peer({
|
peer = self._createPeer({
|
||||||
trickle: false,
|
trickle: false,
|
||||||
config: self.client._rtcConfig,
|
config: self.client._rtcConfig,
|
||||||
wrtc: self.client._wrtc
|
wrtc: self.client._wrtc
|
||||||
@ -389,7 +389,7 @@ WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
|
|||||||
function generateOffer () {
|
function generateOffer () {
|
||||||
var offerId = randombytes(20).toString('hex')
|
var offerId = randombytes(20).toString('hex')
|
||||||
debug('creating peer (from _generateOffers)')
|
debug('creating peer (from _generateOffers)')
|
||||||
var peer = self.peers[offerId] = new Peer({
|
var peer = self.peers[offerId] = self._createPeer({
|
||||||
initiator: true,
|
initiator: true,
|
||||||
trickle: false,
|
trickle: false,
|
||||||
config: self.client._rtcConfig,
|
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 () {}
|
function noop () {}
|
||||||
|
Loading…
Reference in New Issue
Block a user