simple-websocket@2

This commit is contained in:
Feross Aboukhadijeh 2015-04-11 11:58:21 +12:00
parent 0a3c8962a4
commit f302bee0bb
2 changed files with 11 additions and 11 deletions

View File

@ -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)) {

View File

@ -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"