diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index d3a0e21..a734ba8 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -31,6 +31,7 @@ function WebSocketTracker (client, announceUrl, opts) { self.socket = null self.reconnecting = false self.retries = 0 + self.reconnectTimer = null self._openSocket() } @@ -89,17 +90,6 @@ WebSocketTracker.prototype.destroy = function (cb) { clearInterval(self.interval) clearTimeout(self.reconnectTimer) - // Destroy peers - for (var peerId in self.peers) { - var peer = self.peers[peerId] - clearTimeout(peer.trackerTimeout) - peer.destroy() - } - delete self.peers - - // Close socked - if (socketPool[self.announceUrl]) socketPool[self.announceUrl].consumers-- - self.socket.removeListener('connect', self._onSocketConnectBound) self.socket.removeListener('data', self._onSocketDataBound) self.socket.removeListener('close', self._onSocketCloseBound) @@ -110,6 +100,18 @@ WebSocketTracker.prototype.destroy = function (cb) { self._onSocketDataBound = null self._onSocketCloseBound = null + // Destroy peers + for (var peerId in self.peers) { + var peer = self.peers[peerId] + clearTimeout(peer.trackerTimeout) + peer.destroy() + } + self.peers = null + + if (socketPool[self.announceUrl]) { + socketPool[self.announceUrl].consumers -= 1 + } + if (socketPool[self.announceUrl].consumers === 0) { delete socketPool[self.announceUrl] @@ -118,7 +120,7 @@ WebSocketTracker.prototype.destroy = function (cb) { try { self.socket.destroy(cb) } catch (err) { - if (cb) cb() + cb(null) } } @@ -135,12 +137,12 @@ WebSocketTracker.prototype._openSocket = function () { self._onSocketCloseBound = self._onSocketClose.bind(self) self.socket = socketPool[self.announceUrl] - if (!self.socket) { + if (self.socket) { + socketPool[self.announceUrl].consumers += 1 + } else { 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) diff --git a/server.js b/server.js index ab8aa95..c4791ec 100644 --- a/server.js +++ b/server.js @@ -599,7 +599,6 @@ Server.prototype._onWebSocketClose = function (socket) { var swarm = self.torrents[infoHash] if (swarm) { swarm.announce({ - type: 'ws', event: 'stopped', numwant: 0, peer_id: socket.peerId diff --git a/test/server.js b/test/server.js index 30b38de..5b79168 100644 --- a/test/server.js +++ b/test/server.js @@ -8,8 +8,8 @@ var peerId = new Buffer('01234567890123456789') var peerId2 = new Buffer('12345678901234567890') var torrentLength = 50000 -function serverTest (t, serverType, serverFamily) { - t.plan(25) +function serverTest (t, serverType, serverFamily, cb) { + t.plan(30) var hostname = serverFamily === 'inet6' ? '[::1]' @@ -47,17 +47,20 @@ function serverTest (t, serverType, serverFamily) { t.equal(swarm.incomplete, 1) t.equal(Object.keys(swarm.peers).length, 1) - if (serverType !== 'ws') { - t.deepEqual(swarm.peers[hostname + ':6881'], { - type: serverType, - ip: clientIp, - port: 6881, - peerId: peerId.toString('hex'), - complete: false, - socket: undefined - }) + var id = serverType === 'ws' + ? peerId.toString('hex') + : hostname + ':6881' + + t.equal(swarm.peers[id].type, serverType) + t.equal(swarm.peers[id].ip, clientIp) + t.equal(swarm.peers[id].peerId, peerId.toString('hex')) + t.equal(swarm.peers[id].complete, false) + if (serverType === 'ws') { + t.equal(typeof swarm.peers[id].port, 'number') + t.ok(swarm.peers[id].socket) } else { - t.equal(swarm.peers[peerId.toString('hex')].complete, false) + t.equal(swarm.peers[id].port, 6881) + t.notOk(swarm.peers[id].socket) } client1.complete() @@ -104,7 +107,7 @@ function serverTest (t, serverType, serverFamily) { t.equal(data.incomplete, 0) client1.destroy() - server.close() + server.close(cb) }) }) }) @@ -115,19 +118,13 @@ function serverTest (t, serverType, serverFamily) { }) } -test('create daemon', function (t) { - wrtc = require('electron-webrtc')() - wrtc.electronDaemon.once('ready', t.end) -}) - test('websocket server', function (t) { - serverTest(t, 'ws', 'inet') -}) - -// cleanup -test('cleanup electron-eval daemon', function (t) { - wrtc.close() - t.end() + wrtc = require('electron-webrtc')() + wrtc.electronDaemon.once('ready', function () { + serverTest(t, 'ws', 'inet', function () { + wrtc.close() + }) + }) }) test('http ipv4 server', function (t) {