From f302bee0bbcfb9f50d5afe9ed40c430fff9a53dd Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 11 Apr 2015 11:58:21 +1200 Subject: [PATCH] simple-websocket@2 --- lib/websocket-tracker.js | 20 ++++++++++---------- package.json | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/websocket-tracker.js b/lib/websocket-tracker.js index fc2af20..dd6d93d 100644 --- a/lib/websocket-tracker.js +++ b/lib/websocket-tracker.js @@ -27,7 +27,6 @@ function WebSocketTracker (client, announceUrl, opts) { self._opts = opts self._announceUrl = announceUrl self._peers = {} // peers (offer id -> peer) - self._ready = false self._socket = null self._intervalMs = self.client._intervalMs // use client interval initially self._interval = null @@ -35,14 +34,15 @@ function WebSocketTracker (client, announceUrl, opts) { if (socketPool[announceUrl]) self._socket = socketPool[announceUrl] else self._socket = socketPool[announceUrl] = new Socket(announceUrl) - self._socket.on('warning', self._onSocketWarning.bind(self)) - self._socket.on('error', self._onSocketWarning.bind(self)) // TODO: handle error - self._socket.on('message', self._onSocketMessage.bind(self)) + self._socket.on('error', self._onSocketError.bind(self)) + self._socket.on('data', self._onSocketData.bind(self)) } WebSocketTracker.prototype.announce = function (opts) { var self = this - if (!self._socket.ready) return self._socket.on('ready', self.announce.bind(self, opts)) + if (!self._socket.connected) { + return self._socket.on('connect', self.announce.bind(self, opts)) + } opts.info_hash = self.client._infoHash.toString('binary') opts.peer_id = self.client._peerId.toString('binary') @@ -59,8 +59,7 @@ WebSocketTracker.prototype.announce = function (opts) { WebSocketTracker.prototype.scrape = function (opts) { var self = this - self.client.emit('error', new Error('scrape not supported ' + self._announceUrl)) - return + self._onSocketError(new Error('scrape not supported ' + self._announceUrl)) } // TODO: Improve this interface @@ -76,11 +75,12 @@ WebSocketTracker.prototype.setInterval = function (intervalMs) { } } -WebSocketTracker.prototype._onSocketWarning = function (err) { - debug('tracker warning %s', err.message) +WebSocketTracker.prototype._onSocketError = function (err) { + var self = this + self.client.emit('error', err) } -WebSocketTracker.prototype._onSocketMessage = function (data) { +WebSocketTracker.prototype._onSocketData = function (data) { var self = this if (!(typeof data === 'object' && data !== null)) { diff --git a/package.json b/package.json index 0669fe4..b0d4986 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "run-series": "^1.0.2", "simple-get": "^1.3.0", "simple-peer": "^4.0.4", - "simple-websocket": "^1.0.4", + "simple-websocket": "^2.0.0", "string2compact": "^1.1.1", "ws": "^0.7.1", "xtend": "^4.0.0"