mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-31 10:31: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
|
self.destroyed = true
|
||||||
clearInterval(self.interval)
|
clearInterval(self.interval)
|
||||||
|
|
||||||
delete socketPool[self.announceUrl]
|
if (socketPool[self.announceUrl]) socketPool[self.announceUrl].consumers--
|
||||||
|
|
||||||
self.socket.removeListener('connect', self._onSocketConnectBound)
|
self.socket.removeListener('connect', self._onSocketConnectBound)
|
||||||
self.socket.removeListener('data', self._onSocketDataBound)
|
self.socket.removeListener('data', self._onSocketDataBound)
|
||||||
@ -100,11 +100,16 @@ WebSocketTracker.prototype.destroy = function (cb) {
|
|||||||
self._onSocketDataBound = null
|
self._onSocketDataBound = null
|
||||||
self._onSocketCloseBound = null
|
self._onSocketCloseBound = null
|
||||||
|
|
||||||
|
if (socketPool[self.announceUrl].consumers === 0) {
|
||||||
|
delete socketPool[self.announceUrl]
|
||||||
|
|
||||||
self.socket.on('error', noop) // ignore all future errors
|
self.socket.on('error', noop) // ignore all future errors
|
||||||
|
|
||||||
try {
|
try {
|
||||||
self.socket.destroy(cb)
|
self.socket.destroy(cb)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
cb(null)
|
if (cb) cb()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.socket = null
|
self.socket = null
|
||||||
@ -122,7 +127,10 @@ WebSocketTracker.prototype._openSocket = function () {
|
|||||||
self.socket = socketPool[self.announceUrl]
|
self.socket = socketPool[self.announceUrl]
|
||||||
if (!self.socket) {
|
if (!self.socket) {
|
||||||
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
|
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
|
||||||
|
self.socket.consumers = 1
|
||||||
self.socket.on('connect', self._onSocketConnectBound)
|
self.socket.on('connect', self._onSocketConnectBound)
|
||||||
|
} else {
|
||||||
|
socketPool[self.announceUrl].consumers++
|
||||||
}
|
}
|
||||||
|
|
||||||
self.socket.on('data', self._onSocketDataBound)
|
self.socket.on('data', self._onSocketDataBound)
|
||||||
|
@ -599,6 +599,7 @@ Server.prototype._onWebSocketClose = function (socket) {
|
|||||||
var swarm = self.torrents[infoHash]
|
var swarm = self.torrents[infoHash]
|
||||||
if (swarm) {
|
if (swarm) {
|
||||||
swarm.announce({
|
swarm.announce({
|
||||||
|
type: 'ws',
|
||||||
event: 'stopped',
|
event: 'stopped',
|
||||||
numwant: 0,
|
numwant: 0,
|
||||||
peer_id: socket.peerId
|
peer_id: socket.peerId
|
||||||
|
Loading…
Reference in New Issue
Block a user