Merge pull request #34 from tiagosiebler/#21

fix(#21): add pass-thru options and URL override for WebSocket client. Update docs.
This commit is contained in:
Tiago
2020-11-11 21:35:42 +00:00
committed by GitHub
3 changed files with 40 additions and 6 deletions

View File

@@ -52,7 +52,35 @@ const {WebsocketClient} = require('bybit-api');
const API_KEY = 'xxx';
const PRIVATE_KEY = 'yyy';
const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY});
const wsConfig = {
key: API_KEY,
secret: PRIVATE_KEY,
// The following parameters are optional:
// defaults to false == testnet. set to true for livenet.
// livenet: true
// override which URL to use for websocket connections
// wsUrl: 'wss://stream.bytick.com/realtime'
// how often to check (in ms) that WS connection is still alive
// pingInterval: 10000,
// how long to wait (in ms) before deciding the connection should be terminated & reconnected
// pongTimeout: 1000,
// how long to wait before attempting to reconnect (in ms) after connection is closed
// reconnectTimeout: 500,
// config options sent to RestClient (used for time sync). See RestClient docs.
// restOptions: { },
// config for axios to pass to RestClient. E.g for proxy support
// requestOptions: { }
};
const ws = new WebsocketClient(wsConfig);
ws.subscribe(['position', 'execution', 'trade']);
ws.subscribe('kline.BTCUSD.1m');

View File

@@ -1,5 +1,4 @@
const {EventEmitter} = require('events');
const { EventEmitter } = require('events');
const WebSocket = require('ws');
const defaultLogger = require('./logger');
@@ -35,7 +34,7 @@ module.exports = class WebsocketClient extends EventEmitter {
...options
};
this.client = new RestClient(null, null, this.options.livenet);
this.client = new RestClient(null, null, this.options.livenet, this.options.restOptions, this.options.requestOptions);
this._subscriptions = new Set();
this._connect();
@@ -65,12 +64,19 @@ module.exports = class WebsocketClient extends EventEmitter {
this.ws && this.ws.close();
}
_getWsUrl() {
if (this.options.wsUrl) {
return this.options.wsUrl;
}
return wsUrls[this.options.livenet ? 'livenet' : 'testnet'];
}
async _connect() {
try {
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;
const url = this._getWsUrl() + authParams;
this.ws = new WebSocket(url);

View File

@@ -1,6 +1,6 @@
{
"name": "bybit-api",
"version": "1.2.2",
"version": "1.2.3",
"description": "A production-ready Node.js connector for the Bybit APIs and WebSockets",
"main": "index.js",
"scripts": {