mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 02:21:36 +00:00
Close the websocket only when no one is using it
This commit is contained in:
parent
c2115fbf7f
commit
35e6eeaef2
@ -88,7 +88,7 @@ WebSocketTracker.prototype.destroy = function (cb) {
|
||||
self.destroyed = true
|
||||
clearInterval(self.interval)
|
||||
|
||||
delete socketPool[self.announceUrl]
|
||||
if (socketPool[self.announceUrl]) socketPool[self.announceUrl].consumers--
|
||||
|
||||
self.socket.removeListener('connect', self._onSocketConnectBound)
|
||||
self.socket.removeListener('data', self._onSocketDataBound)
|
||||
@ -100,11 +100,16 @@ WebSocketTracker.prototype.destroy = function (cb) {
|
||||
self._onSocketDataBound = null
|
||||
self._onSocketCloseBound = null
|
||||
|
||||
self.socket.on('error', noop) // ignore all future errors
|
||||
try {
|
||||
self.socket.destroy(cb)
|
||||
} catch (err) {
|
||||
cb(null)
|
||||
if (socketPool[self.announceUrl].consumers === 0) {
|
||||
delete socketPool[self.announceUrl]
|
||||
|
||||
self.socket.on('error', noop) // ignore all future errors
|
||||
|
||||
try {
|
||||
self.socket.destroy(cb)
|
||||
} catch (err) {
|
||||
if (cb) cb()
|
||||
}
|
||||
}
|
||||
|
||||
self.socket = null
|
||||
@ -122,7 +127,10 @@ WebSocketTracker.prototype._openSocket = function () {
|
||||
self.socket = socketPool[self.announceUrl]
|
||||
if (!self.socket) {
|
||||
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
|
||||
self.socket.consumers = 1
|
||||
self.socket.on('connect', self._onSocketConnectBound)
|
||||
} else {
|
||||
socketPool[self.announceUrl].consumers++
|
||||
}
|
||||
|
||||
self.socket.on('data', self._onSocketDataBound)
|
||||
|
Loading…
Reference in New Issue
Block a user