Do not crash when wrtc dep is missing

This commit is contained in:
Feross Aboukhadijeh 2015-04-20 16:38:59 -07:00
parent e2d4e8cebf
commit a5d0975dfa
3 changed files with 22 additions and 5 deletions

View File

@ -61,7 +61,8 @@ function Client (peerId, port, torrent, opts) {
return new HTTPTracker(self, announceUrl, trackerOpts)
} else if (protocol === 'udp:' && typeof UDPTracker === 'function') {
return new UDPTracker(self, announceUrl, trackerOpts)
} else if (protocol === 'ws:' || protocol === 'wss:') {
} else if ((protocol === 'ws:' || protocol === 'wss:') &&
WebSocketTracker.supported) {
return new WebSocketTracker(self, announceUrl, trackerOpts)
}
return null

View File

@ -9,6 +9,15 @@ var inherits = require('inherits')
var Peer = require('simple-peer')
var Socket = require('simple-websocket')
var wrtc
try {
wrtc = require('wrtc') // webrtc in node - will be empty object in browser
} catch (err) {
wrtc = null // optional dependency failed to install
}
var WEBRTC_SUPPORT = typeof window !== 'undefined' || !!wrtc
var common = require('./common')
// It turns out that you can't open multiple websockets to the same server within one
@ -38,6 +47,8 @@ function WebSocketTracker (client, announceUrl, opts) {
self._socket.on('data', self._onSocketData.bind(self))
}
WebSocketTracker.supported = WEBRTC_SUPPORT
WebSocketTracker.prototype.announce = function (opts) {
var self = this
if (!self._socket.connected) {
@ -122,7 +133,7 @@ WebSocketTracker.prototype._onSocketData = function (data) {
var peer
if (data.offer) {
peer = new Peer({ trickle: false, config: self._opts.rtcConfig })
peer = new Peer({ trickle: false, config: self._opts.rtcConfig, wrtc: wrtc })
peer.id = common.binaryToHex(data.peer_id)
peer.once('signal', function (answer) {
var params = {
@ -172,7 +183,8 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
var peer = self._peers[offerId] = new Peer({
initiator: true,
trickle: false,
config: self._opts.rtcConfig
config: self._opts.rtcConfig,
wrtc: wrtc
})
peer.once('signal', function (offer) {
offers.push({

View File

@ -14,7 +14,8 @@
"./lib/common-node": false,
"./lib/http-tracker": false,
"./lib/udp-tracker": false,
"./server": false
"./server": false,
"wrtc": false
},
"bugs": {
"url": "https://github.com/feross/bittorrent-tracker/issues"
@ -32,7 +33,7 @@
"once": "^1.3.0",
"run-series": "^1.0.2",
"simple-get": "^1.3.0",
"simple-peer": "^4.0.4",
"simple-peer": "^5.0.0",
"simple-websocket": "^2.0.0",
"string2compact": "^1.1.1",
"ws": "^0.7.1",
@ -63,5 +64,8 @@
},
"scripts": {
"test": "standard && tape test/*.js"
},
"optionalDependencies": {
"wrtc": "0.0.55"
}
}