Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorYoann Ciabaud <yoann@atacma.agency>2015-09-28 02:12:40 +0300
committerYoann Ciabaud <yoann@atacma.agency>2015-11-09 16:31:25 +0300
commit8c14314beea9b3dd2c5dc92a095600724850cbb2 (patch)
tree304a657e59f10bf3eb79be7a024ffb4fc2d867b6 /bin
parent2b696a734fc031ecc375826b5c263079370c8a03 (diff)
Pick an available port instead of crashing with "listen EADDRINUSE" or "listen EACCES" Fixes: #411
Diffstat (limited to 'bin')
-rwxr-xr-xbin/cmd.js21
1 files changed, 17 insertions, 4 deletions
diff --git a/bin/cmd.js b/bin/cmd.js
index 1e39300..77ed919 100755
--- a/bin/cmd.js
+++ b/bin/cmd.js
@@ -310,10 +310,23 @@ function runDownload (torrentId) {
// Start http server
server = torrent.createServer()
- server.listen(argv.port, function () {
+
+ function initServer () {
if (torrent.ready) onReady()
else torrent.once('ready', onReady)
- })
+ }
+
+ server.listen(argv.port, initServer)
+
+ .on('error', function (err) {
+ // In case the port is unusable
+ if (err.code === 'EADDRINUSE') {
+ // Let the OS choose one for us
+ server.listen(0, initServer)
+ }
+ else throw err
+ })
+
server.once('connection', function () {
serving = true
})
@@ -366,8 +379,8 @@ function runDownload (torrentId) {
function onSelection (index) {
href = (argv.airplay || argv.chromecast || argv.xbmc)
- ? 'http://' + networkAddress() + ':' + argv.port + '/' + index
- : 'http://localhost:' + argv.port + '/' + index
+ ? 'http://' + networkAddress() + ':' + server.address().port + '/' + index
+ : 'http://localhost:' + server.address().port + '/' + index
if (playerName) torrent.files[index].select()
if (argv.stdout) torrent.files[index].createReadStream().pipe(process.stdout)