mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-18 12:11:36 +00:00
Merge pull request #220 from feross/handle-errors
Handle peer 'error' events correctly
This commit is contained in:
commit
291df676fd
@ -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 () {}
|
||||
|
Loading…
Reference in New Issue
Block a user