diff --git a/lib/transports/ws2.js b/lib/transports/ws2.js index bdcfb881..2d2f301e 100644 --- a/lib/transports/ws2.js +++ b/lib/transports/ws2.js @@ -1427,9 +1427,23 @@ class WSv2 extends EventEmitter { * @private */ _sendCalc (msg) { - debug('req calc: %j', msg) + debug('req calc (throttled): %j', msg) + this.send(msg) // use send to check if we are still connected + } - 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 + } + + this.send(ping) // use send to check if we are still connected } /** 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', () => {