mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-13 02:46:29 +00:00
parent
9a6e854609
commit
938c025ed9
11
client.js
11
client.js
@ -25,6 +25,7 @@ inherits(Client, EventEmitter)
|
||||
* @param {Number} opts.numWant number of peers to request
|
||||
* @param {Number} opts.interval announce interval (in ms)
|
||||
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
|
||||
* @param {Number} opts.wrtc custom webrtc implementation
|
||||
*/
|
||||
function Client (peerId, port, torrent, opts) {
|
||||
var self = this
|
||||
@ -42,6 +43,9 @@ function Client (peerId, port, torrent, opts) {
|
||||
: new Buffer(torrent.infoHash, 'hex')
|
||||
self.torrentLength = torrent.length
|
||||
|
||||
self._rtcConfig = opts.rtcConfig
|
||||
self._wrtc = opts.wrtc
|
||||
|
||||
// optional
|
||||
self._numWant = opts.numWant || common.DEFAULT_ANNOUNCE_PEERS
|
||||
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
|
||||
@ -51,9 +55,11 @@ function Client (peerId, port, torrent, opts) {
|
||||
if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ]
|
||||
if (torrent.announce == null) torrent.announce = []
|
||||
|
||||
var trackerOpts = { interval: self._intervalMs }
|
||||
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'
|
||||
|
||||
self._trackers = torrent.announce
|
||||
.map(function (announceUrl) {
|
||||
var trackerOpts = { interval: self._intervalMs }
|
||||
var protocol = url.parse(announceUrl).protocol
|
||||
|
||||
if ((protocol === 'http:' || protocol === 'https:') &&
|
||||
@ -61,8 +67,7 @@ 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:') &&
|
||||
WebSocketTracker.supported) {
|
||||
} else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) {
|
||||
return new WebSocketTracker(self, announceUrl, trackerOpts)
|
||||
}
|
||||
return null
|
||||
|
@ -9,16 +9,6 @@ var inherits = require('inherits')
|
||||
var Peer = require('simple-peer')
|
||||
var Socket = require('simple-websocket')
|
||||
|
||||
var wrtc
|
||||
try {
|
||||
wrtc = require('wrtc') // WebRTC in node - empty object in browser
|
||||
if (!wrtc.RTCPeerConnection) wrtc = null
|
||||
} catch (err) {
|
||||
wrtc = null // optional dependency failed to install
|
||||
}
|
||||
|
||||
var WEBRTC_SUPPORT = !!wrtc || typeof window !== 'undefined'
|
||||
|
||||
var common = require('./common')
|
||||
|
||||
// It turns out that you can't open multiple websockets to the same server within one
|
||||
@ -48,8 +38,6 @@ 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) {
|
||||
@ -138,7 +126,11 @@ WebSocketTracker.prototype._onSocketData = function (data) {
|
||||
|
||||
var peer
|
||||
if (data.offer) {
|
||||
peer = new Peer({ trickle: false, config: self._opts.rtcConfig, wrtc: wrtc })
|
||||
peer = new Peer({
|
||||
trickle: false,
|
||||
config: self.client._rtcConfig,
|
||||
wrtc: self.client._wrtc
|
||||
})
|
||||
peer.id = common.binaryToHex(data.peer_id)
|
||||
peer.once('signal', function (answer) {
|
||||
var params = {
|
||||
@ -188,8 +180,8 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
|
||||
var peer = self._peers[offerId] = new Peer({
|
||||
initiator: true,
|
||||
trickle: false,
|
||||
config: self._opts.rtcConfig,
|
||||
wrtc: wrtc
|
||||
config: self.client._rtcConfig,
|
||||
wrtc: self.client._wrtc
|
||||
})
|
||||
peer.once('signal', function (offer) {
|
||||
offers.push({
|
||||
|
@ -14,8 +14,7 @@
|
||||
"./lib/common-node": false,
|
||||
"./lib/http-tracker": false,
|
||||
"./lib/udp-tracker": false,
|
||||
"./server": false,
|
||||
"wrtc": false
|
||||
"./server": false
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/feross/bittorrent-tracker/issues"
|
||||
@ -64,8 +63,5 @@
|
||||
},
|
||||
"scripts": {
|
||||
"test": "standard && tape test/*.js"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
"wrtc": "0.0.55"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user