From ea9904ba761b42a95adab958fb47b62e94e82622 Mon Sep 17 00:00:00 2001 From: Carsten Wolters Date: Thu, 13 Dec 2018 16:14:46 +0100 Subject: [PATCH 1/5] added ping method (to receive a pong, and don't trigger re-connect if nothing happens) --- lib/transports/ws2.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/transports/ws2.js b/lib/transports/ws2.js index bdcfb881..7899ea65 100644 --- a/lib/transports/ws2.js +++ b/lib/transports/ws2.js @@ -1432,6 +1432,20 @@ class WSv2 extends EventEmitter { this._ws.send(JSON.stringify(msg)) } + /** + * send a ping (to receive a pong) + * https://docs.bitfinex.com/v2/docs/ws-general#section-ping-pong + * @param {number} cid + */ + ping (cid) { + const ping = { + "event":"ping", + "cid": cid + } + debug('send ping: %j', ping) + this. _ws.send(JSON.stringify(ping)) + } + /** * Sends a new order to the server and resolves the returned promise once the * order submit is confirmed. Emits an error if not authenticated. The order From 9acbf6e7d0e2e5626cfdd8d0ad58d21ef259b945 Mon Sep 17 00:00:00 2001 From: Carsten Wolters Date: Thu, 13 Dec 2018 16:24:03 +0100 Subject: [PATCH 2/5] fixed " --- lib/transports/ws2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/transports/ws2.js b/lib/transports/ws2.js index 7899ea65..87a24b59 100644 --- a/lib/transports/ws2.js +++ b/lib/transports/ws2.js @@ -1439,8 +1439,8 @@ class WSv2 extends EventEmitter { */ ping (cid) { const ping = { - "event":"ping", - "cid": cid + 'event':'ping', + 'cid': cid } debug('send ping: %j', ping) this. _ws.send(JSON.stringify(ping)) From 29226097e885d2555db3859cfcb16746c33e34ab Mon Sep 17 00:00:00 2001 From: Carsten Wolters Date: Thu, 13 Dec 2018 16:27:02 +0100 Subject: [PATCH 3/5] fixed " --- lib/transports/ws2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/transports/ws2.js b/lib/transports/ws2.js index 87a24b59..d50dd032 100644 --- a/lib/transports/ws2.js +++ b/lib/transports/ws2.js @@ -1439,11 +1439,11 @@ class WSv2 extends EventEmitter { */ ping (cid) { const ping = { - 'event':'ping', + 'event': 'ping', 'cid': cid } debug('send ping: %j', ping) - this. _ws.send(JSON.stringify(ping)) + this._ws.send(JSON.stringify(ping)) } /** From 4152a6f0dfd58c79ed4df45407095e5437c5ac06 Mon Sep 17 00:00:00 2001 From: Carsten Wolters Date: Fri, 14 Dec 2018 10:50:39 +0100 Subject: [PATCH 4/5] use send() in first place instead of ws._send() to get an additional check if the ws is still connected --- lib/transports/ws2.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/transports/ws2.js b/lib/transports/ws2.js index d50dd032..2d2f301e 100644 --- a/lib/transports/ws2.js +++ b/lib/transports/ws2.js @@ -1427,14 +1427,14 @@ class WSv2 extends EventEmitter { * @private */ _sendCalc (msg) { - debug('req calc: %j', msg) - - this._ws.send(JSON.stringify(msg)) + debug('req calc (throttled): %j', msg) + this.send(msg) // use send to check if we are still connected } /** * send a ping (to receive a pong) * https://docs.bitfinex.com/v2/docs/ws-general#section-ping-pong + * * @param {number} cid */ ping (cid) { @@ -1442,8 +1442,8 @@ class WSv2 extends EventEmitter { 'event': 'ping', 'cid': cid } - debug('send ping: %j', ping) - this._ws.send(JSON.stringify(ping)) + + this.send(ping) // use send to check if we are still connected } /** From 18fabeb6f3700160fb9616291212e43093ce46b2 Mon Sep 17 00:00:00 2001 From: Carsten Wolters Date: Fri, 14 Dec 2018 18:44:37 +0100 Subject: [PATCH 5/5] fixed unit tests - _sendCalc is now using sendMessage - and this needs an open connection --- test/lib/transports/ws2-unit.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/test/lib/transports/ws2-unit.js b/test/lib/transports/ws2-unit.js index 13ae96c5..dc2acb29 100644 --- a/test/lib/transports/ws2-unit.js +++ b/test/lib/transports/ws2-unit.js @@ -58,15 +58,21 @@ describe('WSv2 utilities', () => { }) it('_sendCalc: stringifes payload & passes it to the ws client', (done) => { - const ws = new WSv2() + const wss = new MockWSv2Server() + const ws = createTestWSv2Instance() - ws._ws = {} - ws._ws.send = (data) => { - assert.equal(data, '[]') - done() - } + ws.on('open', () => { - ws._sendCalc([]) + ws._ws.send = (data) => { + wss.close() + assert.equal(data, '[]') + done() + } + + ws._sendCalc([]) + }) + + ws.open() }) it('notifyUI: throws error if supplied invalid arguments', () => {