server: more thorough socket cleanup

This commit is contained in:
Feross Aboukhadijeh 2016-03-18 15:06:05 -07:00
parent 5ee3f22e2b
commit 0740f92d7c

View File

@ -301,9 +301,15 @@ Server.prototype.onWebSocketConnection = function (socket, opts) {
socket.peerId = null // as hex socket.peerId = null // as hex
socket.infoHashes = [] // swarms that this socket is participating in socket.infoHashes = [] // swarms that this socket is participating in
socket.onSend = self._onWebSocketSend.bind(self, socket) socket.onSend = self._onWebSocketSend.bind(self, socket)
socket.on('message', self._onWebSocketRequest.bind(self, socket, opts))
socket.on('error', self._onWebSocketError.bind(self, socket)) socket.onMessageBound = self._onWebSocketRequest.bind(self, socket, opts)
socket.on('close', self._onWebSocketClose.bind(self, socket)) socket.on('message', self.onMessageBound)
socket.onErrorBound = self._onWebSocketError.bind(self, socket)
socket.on('error', self.onErrorBound)
socket.onCloseBound = self._onWebSocketClose.bind(self, socket)
socket.on('close', self.onCloseBound)
} }
Server.prototype._onWebSocketRequest = function (socket, opts, params) { Server.prototype._onWebSocketRequest = function (socket, opts, params) {
@ -598,19 +604,27 @@ Server.prototype._onWebSocketSend = function (socket, err) {
Server.prototype._onWebSocketClose = function (socket) { Server.prototype._onWebSocketClose = function (socket) {
var self = this var self = this
if (!socket.peerId || !socket.infoHashes) return debug('websocket close %s', socket.peerId)
debug('websocket close')
socket.infoHashes.forEach(function (infoHash) { if (socket.peerId) {
var swarm = self.torrents[infoHash] socket.infoHashes.forEach(function (infoHash) {
if (swarm) { var swarm = self.torrents[infoHash]
swarm.announce({ if (swarm) {
event: 'stopped', swarm.announce({
numwant: 0, event: 'stopped',
peer_id: socket.peerId numwant: 0,
}, function () {}) peer_id: socket.peerId
} }, function () {})
}) }
})
}
socket.peerId = null
socket.infoHashes = null
socket.onSend = null
socket.removeListener('message', socket.onMessageBound)
socket.removeListener('error', socket.onErrorBound)
socket.removeListener('close', socket.onCloseBound)
} }
Server.prototype._onWebSocketError = function (socket, err) { Server.prototype._onWebSocketError = function (socket, err) {