feat: start work on axios integration, deprecating request.

This commit is contained in:
tiagosiebler
2020-10-04 14:22:59 +01:00
parent 327007b307
commit b201d364bf
13 changed files with 5488 additions and 58 deletions

View File

@@ -2,9 +2,9 @@ const {EventEmitter} = require('events');
const WebSocket = require('ws');
const defaultLogger = require('./logger.js');
const RestClient = require('./rest-client.js');
const {signMessage} = require('./utility.js');
const defaultLogger = require('./logger');
const RestClient = require('./rest-client');
const { signMessage } = require('./utility');
const wsUrls = {
livenet: 'wss://stream.bybit.com/realtime',
@@ -33,7 +33,7 @@ module.exports = class WebsocketClient extends EventEmitter {
pingInterval: 10000,
reconnectTimeout: 500,
...options
}
};
this.client = new RestClient(null, null, this.options.livenet);
this._subscriptions = new Set();
@@ -42,20 +42,20 @@ module.exports = class WebsocketClient extends EventEmitter {
}
subscribe(topics) {
if(!Array.isArray(topics)) topics = [topics];
if (!Array.isArray(topics)) topics = [topics];
topics.forEach(topic => this._subscriptions.add(topic));
// subscribe not necessary if not yet connected (will subscribe onOpen)
if(this.readyState === READY_STATE_CONNECTED) this._subscribe(topics);
if (this.readyState === READY_STATE_CONNECTED) this._subscribe(topics);
}
unsubscribe(topics) {
if(!Array.isArray(topics)) topics = [topics];
if (!Array.isArray(topics)) topics = [topics];
topics.forEach(topic => this._subscriptions.delete(topic));
// unsubscribe not necessary if not yet connected
if(this.readyState === READY_STATE_CONNECTED) this._unsubscribe(topics);
if (this.readyState === READY_STATE_CONNECTED) this._unsubscribe(topics);
}
close() {
@@ -67,7 +67,7 @@ module.exports = class WebsocketClient extends EventEmitter {
async _connect() {
try {
if(this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
if (this.readyState === READY_STATE_INITIAL) this.readyState = READY_STATE_CONNECTING;
const authParams = await this._authenticate();
const url = wsUrls[this.options.livenet ? 'livenet' : 'testnet'] + authParams;
@@ -78,14 +78,14 @@ module.exports = class WebsocketClient extends EventEmitter {
this.ws.on('message', this._wsMessageHandler.bind(this));
this.ws.on('error', this._wsOnErrorHandler.bind(this));
this.ws.on('close', this._wsCloseHandler.bind(this));
} catch(err) {
} catch (err) {
this.logger.error('Connection failed', err);
this._reconnect(this.options.reconnectTimeout);
}
}
async _authenticate() {
if(this.options.key && this.options.secret) {
if (this.options.key && this.options.secret) {
this.logger.debug('Starting authenticated websocket client.', {category: 'bybit-ws'});
const timeOffset = await this.client.getTimeOffset();
@@ -101,7 +101,7 @@ module.exports = class WebsocketClient extends EventEmitter {
.sort()
.map(key => `${key}=${params[key]}`)
.join('&');
} else if(this.options.key || this.options.secret) {
} 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 {
this.logger.debug('Starting public only websocket client.', {category: 'bybit-ws'});
@@ -112,7 +112,7 @@ 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'});
@@ -136,18 +136,18 @@ module.exports = class WebsocketClient extends EventEmitter {
}
_teardown() {
if(this.pingInterval) clearInterval(this.pingInterval);
if(this.pongTimeout) clearTimeout(this.pongTimeout);
if (this.pingInterval) clearInterval(this.pingInterval);
if (this.pongTimeout) clearTimeout(this.pongTimeout);
this.pongTimeout = null;
this.pingInterval = null;
}
_wsOpenHandler() {
if(this.readyState === READY_STATE_CONNECTING) {
if (this.readyState === READY_STATE_CONNECTING) {
this.logger.info('Websocket connected', {category: 'bybit-ws', livenet: this.options.livenet});
this.emit('open');
} else if(this.readyState === READY_STATE_RECONNECTING) {
} else if (this.readyState === READY_STATE_RECONNECTING) {
this.logger.info('Websocket reconnected', {category: 'bybit-ws', livenet: this.options.livenet});
this.emit('reconnected');
}
@@ -161,9 +161,9 @@ module.exports = class WebsocketClient extends EventEmitter {
_wsMessageHandler(message) {
let msg = JSON.parse(message);
if('success' in msg) {
if ('success' in msg) {
this._handleResponse(msg);
} else if(msg.topic) {
} else if (msg.topic) {
this._handleUpdate(msg);
} else {
this.logger.warning('Got unhandled ws message', msg);
@@ -172,13 +172,13 @@ module.exports = class WebsocketClient extends EventEmitter {
_wsOnErrorHandler(err) {
this.logger.error('Websocket error', {category: 'bybit-ws', err});
if(this.readyState === READY_STATE_CONNECTED) this.emit('error', err);
if (this.readyState === READY_STATE_CONNECTED) this.emit('error', err);
}
_wsCloseHandler() {
this.logger.info('Websocket connection closed', {category: 'bybit-ws'});
if(this.readyState !== READY_STATE_CLOSING) {
if (this.readyState !== READY_STATE_CLOSING) {
this._reconnect(this.options.reconnectTimeout);
this.emit('reconnect');
} else {
@@ -188,8 +188,8 @@ 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') {
if (response.success === true) {
this.logger.silly('pong recieved', {category: 'bybit-ws'});
clearTimeout(this.pongTimeout);
}
@@ -219,4 +219,4 @@ module.exports = class WebsocketClient extends EventEmitter {
this.ws.send(msgStr);
}
}
};