diff --git a/client.js b/client.js index 42567e6..4884696 100644 --- a/client.js +++ b/client.js @@ -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 diff --git a/lib/websocket-tracker.js b/lib/websocket-tracker.js index dd6d93d..17ac73f 100644 --- a/lib/websocket-tracker.js +++ b/lib/websocket-tracker.js @@ -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({ diff --git a/package.json b/package.json index 49becc7..cb85da0 100644 --- a/package.json +++ b/package.json @@ -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" } }