Skip to content

Commit ee59577

Browse files
committed
Generates and queues offers if socket is not connected. Once socket connects, queued offers are sent.
1 parent 7bd6ce0 commit ee59577

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

lib/client/websocket-tracker.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class WebSocketTracker extends Tracker {
2626

2727
this.trickle = opts.trickle !== undefined ? opts.trickle : false
2828
this.peers = {} // peers (offer id -> peer)
29+
this.pendingWrites = []
2930
this.socket = null
3031

3132
this.reconnecting = false
@@ -43,16 +44,19 @@ class WebSocketTracker extends Tracker {
4344
if (this.destroyed || this.reconnecting) return
4445
if (!this.socket.connected) {
4546
this.socket.once('connect', () => {
46-
this.announce(opts)
47+
// Send writes in FIFO order.
48+
while (this.pendingWrites.length) {
49+
const write = this.pendingWrites.shift()
50+
this._send(write)
51+
}
4752
})
48-
return
4953
}
50-
51-
const params = Object.assign({}, opts, {
52-
action: 'announce',
53-
info_hash: this.client._infoHashBinary,
54-
peer_id: this.client._peerIdBinary,
55-
})
54+
const baseParams = {
55+
action: 'announce',
56+
info_hash: this.client._infoHashBinary,
57+
peer_id: this.client._peerIdBinary,
58+
}
59+
const params = Object.assign({}, opts, baseParams)
5660
if (this._trackerId) params.trackerid = this._trackerId
5761

5862
if (opts.event === 'stopped' || opts.event === 'completed') {
@@ -66,15 +70,10 @@ class WebSocketTracker extends Tracker {
6670
params.offers = offers
6771
this._send(params)
6872
}, offer => {
69-
let offerParams = Object.assign({}, params)
70-
offerParams.offers = [offer]
71-
offerParams.event = 'trickle'
72-
73-
delete offerParams.left
74-
delete offerParams.numwant
75-
delete offerParams.uploaded
76-
delete offerParams.downloaded
77-
this._send(offerParams)
73+
const trickleParams = Object.assign({}, baseParams)
74+
trickleParams.offers = [offer]
75+
trickleParams.event = 'trickle'
76+
this._send(trickleParams)
7877
})
7978
} else {
8079
// Limit the number of offers that are generated, since it can be slow
@@ -401,6 +400,10 @@ class WebSocketTracker extends Tracker {
401400

402401
_send (params) {
403402
if (this.destroyed) return
403+
if (!this.socket.connected) {
404+
this.pendingWrites.push(params)
405+
return
406+
}
404407
this.expectingResponse = true
405408
const message = JSON.stringify(params)
406409
debug('send %s', message)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bittorrent-tracker",
33
"description": "Simple, robust, BitTorrent tracker (client & server) implementation",
4-
"version": "9.14.2",
4+
"version": "9.14.2-arc-1",
55
"author": {
66
"name": "WebTorrent, LLC",
77
"email": "[email protected]",

0 commit comments

Comments
 (0)