mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 10:31:36 +00:00
better 'listening' event handling
This commit is contained in:
parent
dce7205f40
commit
3d3c0d44d0
@ -25,7 +25,6 @@
|
|||||||
"inherits": "^2.0.1",
|
"inherits": "^2.0.1",
|
||||||
"ip": "^0.3.0",
|
"ip": "^0.3.0",
|
||||||
"once": "^1.3.0",
|
"once": "^1.3.0",
|
||||||
"run-parallel": "^1.0.0",
|
|
||||||
"string2compact": "^1.1.1"
|
"string2compact": "^1.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
31
server.js
31
server.js
@ -9,7 +9,6 @@ var EventEmitter = require('events').EventEmitter
|
|||||||
var http = require('http')
|
var http = require('http')
|
||||||
var inherits = require('inherits')
|
var inherits = require('inherits')
|
||||||
var ipLib = require('ip')
|
var ipLib = require('ip')
|
||||||
var parallel = require('run-parallel')
|
|
||||||
var string2compact = require('string2compact')
|
var string2compact = require('string2compact')
|
||||||
|
|
||||||
var NUM_ANNOUNCE_PEERS = 50
|
var NUM_ANNOUNCE_PEERS = 50
|
||||||
@ -44,6 +43,7 @@ function Server (opts) {
|
|||||||
|
|
||||||
self._trustProxy = !!opts.trustProxy
|
self._trustProxy = !!opts.trustProxy
|
||||||
|
|
||||||
|
self.port = null
|
||||||
self.torrents = {}
|
self.torrents = {}
|
||||||
|
|
||||||
// default to starting an http server unless the user explictly says no
|
// default to starting an http server unless the user explictly says no
|
||||||
@ -51,6 +51,7 @@ function Server (opts) {
|
|||||||
self._httpServer = http.createServer()
|
self._httpServer = http.createServer()
|
||||||
self._httpServer.on('request', self._onHttpRequest.bind(self))
|
self._httpServer.on('request', self._onHttpRequest.bind(self))
|
||||||
self._httpServer.on('error', self._onError.bind(self))
|
self._httpServer.on('error', self._onError.bind(self))
|
||||||
|
self._httpServer.on('listening', onListening)
|
||||||
}
|
}
|
||||||
|
|
||||||
// default to starting a udp server unless the user explicitly says no
|
// default to starting a udp server unless the user explicitly says no
|
||||||
@ -58,6 +59,13 @@ function Server (opts) {
|
|||||||
self._udpServer = dgram.createSocket('udp4')
|
self._udpServer = dgram.createSocket('udp4')
|
||||||
self._udpServer.on('message', self._onUdpRequest.bind(self))
|
self._udpServer.on('message', self._onUdpRequest.bind(self))
|
||||||
self._udpServer.on('error', self._onError.bind(self))
|
self._udpServer.on('error', self._onError.bind(self))
|
||||||
|
self._udpServer.on('listening', onListening)
|
||||||
|
}
|
||||||
|
|
||||||
|
var num = !!self._httpServer + !!self._udpServer
|
||||||
|
function onListening () {
|
||||||
|
num -= 1
|
||||||
|
if (num === 0) self.emit('listening', self.port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,23 +76,10 @@ Server.prototype._onError = function (err) {
|
|||||||
|
|
||||||
Server.prototype.listen = function (port, onlistening) {
|
Server.prototype.listen = function (port, onlistening) {
|
||||||
var self = this
|
var self = this
|
||||||
var tasks = []
|
self.port = port
|
||||||
|
if (onlistening) self.once('listening', onlistening)
|
||||||
if (onlistening) {
|
self._httpServer && self._httpServer.listen(port.http || port)
|
||||||
self.once('listening', onlistening)
|
self._udpServer && self._udpServer.bind(port.udp || port)
|
||||||
}
|
|
||||||
|
|
||||||
self._httpServer && tasks.push(function (cb) {
|
|
||||||
self._httpServer.listen(port.http || port, cb)
|
|
||||||
})
|
|
||||||
self._udpServer && tasks.push(function (cb) {
|
|
||||||
self._udpServer.bind(port.udp || port, cb)
|
|
||||||
})
|
|
||||||
|
|
||||||
parallel(tasks, function (err) {
|
|
||||||
if (err) return self.emit('error', err)
|
|
||||||
self.emit('listening', port)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.prototype.close = function (cb) {
|
Server.prototype.close = function (cb) {
|
||||||
|
Loading…
Reference in New Issue
Block a user