Added option for linear [BROKEN]
This commit is contained in:
@@ -1,15 +1,21 @@
|
|||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { InverseClient } from './inverse-client';
|
import { InverseClient } from './inverse-client';
|
||||||
|
import { LinearClient } from './linear-client';
|
||||||
import { DefaultLogger } from './logger';
|
import { DefaultLogger } from './logger';
|
||||||
import { signMessage, serializeParams } from './util/requestUtils';
|
import { signMessage, serializeParams } from './util/requestUtils';
|
||||||
// import WebSocket from 'ws';
|
// import WebSocket from 'ws';
|
||||||
import WebSocket from 'isomorphic-ws';
|
import WebSocket from 'isomorphic-ws';
|
||||||
|
|
||||||
const wsUrls = {
|
const iwsUrls = {
|
||||||
livenet: 'wss://stream.bybit.com/realtime',
|
livenet: 'wss://stream.bybit.com/realtime',
|
||||||
testnet: 'wss://stream-testnet.bybit.com/realtime'
|
testnet: 'wss://stream-testnet.bybit.com/realtime'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const lwsUrls = {
|
||||||
|
livenet: 'wss://stream.bybit.com/realtime_public',
|
||||||
|
testnet: 'wss://stream-testnet.bybit.com/realtime_public'
|
||||||
|
};
|
||||||
|
|
||||||
const READY_STATE_INITIAL = 0;
|
const READY_STATE_INITIAL = 0;
|
||||||
const READY_STATE_CONNECTING = 1;
|
const READY_STATE_CONNECTING = 1;
|
||||||
const READY_STATE_CONNECTED = 2;
|
const READY_STATE_CONNECTED = 2;
|
||||||
@@ -20,7 +26,7 @@ export interface WebsocketClientOptions {
|
|||||||
key?: string;
|
key?: string;
|
||||||
secret?: string;
|
secret?: string;
|
||||||
livenet?: boolean;
|
livenet?: boolean;
|
||||||
|
linear?: boolean;
|
||||||
pongTimeout?: number;
|
pongTimeout?: number;
|
||||||
pingInterval?: number;
|
pingInterval?: number;
|
||||||
reconnectTimeout?: number;
|
reconnectTimeout?: number;
|
||||||
@@ -31,12 +37,14 @@ export interface WebsocketClientOptions {
|
|||||||
|
|
||||||
type Logger = typeof DefaultLogger;
|
type Logger = typeof DefaultLogger;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export class WebsocketClient extends EventEmitter {
|
export class WebsocketClient extends EventEmitter {
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
private readyState: number;
|
private readyState: number;
|
||||||
private pingInterval?: number | undefined;
|
private pingInterval?: number | undefined;
|
||||||
private pongTimeout?: number | undefined;
|
private pongTimeout?: number | undefined;
|
||||||
private client: InverseClient;
|
private client: InverseClient | LinearClient;
|
||||||
private _subscriptions: Set<unknown>;
|
private _subscriptions: Set<unknown>;
|
||||||
private ws: WebSocket;
|
private ws: WebSocket;
|
||||||
private options: WebsocketClientOptions;
|
private options: WebsocketClientOptions;
|
||||||
@@ -51,17 +59,25 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
this.options = {
|
this.options = {
|
||||||
livenet: false,
|
livenet: false,
|
||||||
|
linear: false,
|
||||||
pongTimeout: 1000,
|
pongTimeout: 1000,
|
||||||
pingInterval: 10000,
|
pingInterval: 10000,
|
||||||
reconnectTimeout: 500,
|
reconnectTimeout: 500,
|
||||||
...options
|
...options
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
if (this.options.linear) {
|
||||||
this.client = new InverseClient(undefined, undefined, this.options.livenet, this.options.restOptions, this.options.requestOptions);
|
this.client = new InverseClient(undefined, undefined, this.options.livenet, this.options.restOptions, this.options.requestOptions);
|
||||||
|
}else{
|
||||||
|
this.client = new LinearClient(undefined, undefined, this.options.livenet, this.options.restOptions, this.options.requestOptions);
|
||||||
|
}
|
||||||
|
|
||||||
this._subscriptions = new Set();
|
this._subscriptions = new Set();
|
||||||
this._connect();
|
this._connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
subscribe(topics) {
|
subscribe(topics) {
|
||||||
if (!Array.isArray(topics)) topics = [topics];
|
if (!Array.isArray(topics)) topics = [topics];
|
||||||
topics.forEach(topic => this._subscriptions.add(topic));
|
topics.forEach(topic => this._subscriptions.add(topic));
|
||||||
@@ -90,7 +106,10 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
if (this.options.wsUrl) {
|
if (this.options.wsUrl) {
|
||||||
return this.options.wsUrl;
|
return this.options.wsUrl;
|
||||||
}
|
}
|
||||||
return wsUrls[this.options.livenet ? 'livenet' : 'testnet'];
|
if (this.options.linear){
|
||||||
|
return lwsUrls[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
|
}
|
||||||
|
return iwsUrls[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
}
|
}
|
||||||
|
|
||||||
async _connect() {
|
async _connect() {
|
||||||
|
|||||||
Reference in New Issue
Block a user