feat: expose request options in REST constructor. Fix readme links.

This commit is contained in:
tiagosiebler
2020-10-08 22:05:22 +01:00
parent df576407b4
commit 1c14595fd6
3 changed files with 27 additions and 16 deletions

View File

@@ -4,7 +4,7 @@ const WebSocket = require('ws');
const defaultLogger = require('./logger');
const RestClient = require('./rest-client');
const { signMessage } = require('./util/requestUtils');
const { signMessage, serializeParams } = require('./util/requestUtils');
const wsUrls = {
livenet: 'wss://stream.bybit.com/realtime',
@@ -96,11 +96,8 @@ module.exports = class WebsocketClient extends EventEmitter {
};
params.signature = signMessage('GET/realtime' + params.expires, this.options.secret);
return '?' + serializeParams(params);
return '?' + Object.keys(params)
.sort()
.map(key => `${key}=${params[key]}`)
.join('&');
} else if (this.options.key || this.options.secret) {
this.logger.warning('Could not authenticate websocket, either api key or private key missing.', {category: 'bybit-ws'});
} else {
@@ -112,7 +109,9 @@ module.exports = class WebsocketClient extends EventEmitter {
_reconnect(timeout) {
this._teardown();
if (this.readyState !== READY_STATE_CONNECTING) this.readyState = READY_STATE_RECONNECTING;
if (this.readyState !== READY_STATE_CONNECTING) {
this.readyState = READY_STATE_RECONNECTING;
}
setTimeout(() => {
this.logger.info('Reconnecting to server', {category: 'bybit-ws'});
@@ -159,7 +158,7 @@ module.exports = class WebsocketClient extends EventEmitter {
}
_wsMessageHandler(message) {
let msg = JSON.parse(message);
const msg = JSON.parse(message);
if ('success' in msg) {
this._handleResponse(msg);
@@ -188,11 +187,14 @@ module.exports = class WebsocketClient extends EventEmitter {
}
_handleResponse(response) {
if (response.request && response.request.op === 'ping' && response.ret_msg === 'pong') {
if (response.success === true) {
if (
response.request &&
response.request.op === 'ping' &&
response.ret_msg === 'pong' &&
response.success === true
) {
this.logger.silly('pong recieved', {category: 'bybit-ws'});
clearTimeout(this.pongTimeout);
}
} else {
this.emit('response', response);
}