Skip to content

Commit e550d6a

Browse files
committed
UPDATE: simplify http server listener handling
1 parent 8df2e26 commit e550d6a

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

server/index.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,9 @@ class Server extends EventEmitter {
6161
this.peersCacheLength = peersCacheLength
6262
this.peersCacheTtl = peersCacheTtl
6363

64-
if (opts.http !== false) attachHttpService(this, onListening)
65-
if (opts.ws !== false) attachWSService(this, onListening)
66-
if (opts.stats !== false) setupStatsRoute(this, onListening)
67-
68-
// TODO: UGH
69-
let num = !!this.http
70-
this.num = num
71-
72-
const self = this
73-
function onListening () {
74-
num -= 1
75-
if (num === 0) {
76-
self.listening = true
77-
debug('listening')
78-
self.emit('listening')
79-
}
80-
}
64+
if (opts.http !== false) attachHttpService(this)
65+
if (opts.ws !== false) attachWSService(this)
66+
if (opts.stats !== false) setupStatsRoute(this)
8167
}
8268

8369
onError (err) {

server/services/attachHttp/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
const debug = require('debug')('bittorrent-tracker:server')
2+
13
const http = require('http')
24
const bencode = require('bencode')
35

46
const common = require('../../../lib/common')
57
const parseHttpRequest = require('./parseHttpRequest')
68

7-
function attachHttpServer (server, onListening) {
8-
const {
9-
onError
10-
} = server
11-
9+
function attachHttpServer (server) {
1210
const httpServer = http.createServer()
1311

12+
const onListening = () => {
13+
server.listening = true
14+
debug('listening')
15+
server.emit('listening')
16+
}
17+
18+
const onError = (...args) => { server.onError(...args)}
19+
20+
1421
httpServer.on('error', onError)
1522
httpServer.on('listening', onListening)
1623

0 commit comments

Comments
 (0)