diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 11:15:44 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 11:15:44 +0300 |
commit | 08baee2ddd7a194d44fc797e704b7a6b71304423 (patch) | |
tree | d09791a359a25a85c456a560f35a148bd53b35d2 | |
parent | 63e4aee7bd016f258d81708af9c03cf608968816 (diff) |
set dhtPort to correct port after listening
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | index.js | 11 | ||||
-rw-r--r-- | test/node/download-dht-magnet.js | 6 | ||||
-rw-r--r-- | test/node/download-dht-torrent.js | 6 |
4 files changed, 21 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a5c07..a0d574d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - `client.listening` property to signal whether TCP server is listening for incoming connections. +- `client.dhtPort` property reflects the actual DHT port when user doesn't specify one + (this is parallel to `client.torrentPort` for the TCP torrent listening server) + ### Changed - Merged `Swarm` class into `Torrent` object. Properties on `torrent.swarm` (like @@ -1,5 +1,4 @@ // TODO: cleanup event listeners -// TODO: set dhtPort to correct port module.exports = WebTorrent @@ -70,6 +69,7 @@ function WebTorrent (opts) { self.destroyed = false self.listening = false self.torrentPort = opts.torrentPort || 0 + self.dhtPort = opts.dhtPort || 0 self.tracker = opts.tracker !== undefined ? opts.tracker : true self.torrents = [] self.maxConns = Number(opts.maxConns) || 55 @@ -95,11 +95,15 @@ function WebTorrent (opts) { self.dht.once('error', function (err) { self._destroy(err) }) + self.dht.once('listening', function () { + var address = self.dht.address() + if (address) self.dhtPort = address.port + }) // Ignore warning when there are > 10 torrents in the client self.dht.setMaxListeners(0) - self.dht.listen(opts.dhtPort) + self.dht.listen(self.dhtPort) } else { self.dht = false } @@ -387,7 +391,8 @@ WebTorrent.prototype._onListening = function () { if (this._tcpPool) { // Sometimes server.address() returns `null` in Docker. // WebTorrent issue: https://github.com/feross/bittorrent-swarm/pull/18 - this.torrentPort = (this._tcpPool.server.address() || { port: 0 }).port + var address = this._tcpPool.server.address() + if (address) this.torrentPort = address.port } this.emit('listening') diff --git a/test/node/download-dht-magnet.js b/test/node/download-dht-magnet.js index 028d6a6..760bb26 100644 --- a/test/node/download-dht-magnet.js +++ b/test/node/download-dht-magnet.js @@ -7,7 +7,7 @@ var test = require('tape') var WebTorrent = require('../../') test('Download using DHT (via magnet uri)', function (t) { - t.plan(11) + t.plan(12) var dhtServer = new DHT({ bootstrap: false }) @@ -27,6 +27,10 @@ test('Download using DHT (via magnet uri)', function (t) { dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port, host: networkAddress.ipv4() } }) + client1.dht.on('listening', function () { + t.equal(client1.dhtPort, client1.dht.address().port) + }) + client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) diff --git a/test/node/download-dht-torrent.js b/test/node/download-dht-torrent.js index 5f88da5..20c7b0c 100644 --- a/test/node/download-dht-torrent.js +++ b/test/node/download-dht-torrent.js @@ -6,7 +6,7 @@ var test = require('tape') var WebTorrent = require('../../') test('Download using DHT (via .torrent file)', function (t) { - t.plan(8) + t.plan(9) var dhtServer = new DHT({ bootstrap: false }) @@ -26,6 +26,10 @@ test('Download using DHT (via .torrent file)', function (t) { dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } }) + client1.dht.on('listening', function () { + t.equal(client1.dhtPort, client1.dht.address().port) + }) + client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) |