Skip to content

Commit

Permalink
Use timeoutScheduler in webSocket clients
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Sep 5, 2022
1 parent 6ac6c99 commit 6fab5f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/clients/webSocket/exchangeWebSocketClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AuthorizationManager, AuthToken } from '../../authorization/index';
import { EventEmitter } from '../../core/index';
import { EventEmitter, TimeoutScheduler } from '../../core/index';
import type { WebSocketResponseDto } from '../dtos';
import { WebSocketClient } from './webSocketClient';

Expand All @@ -17,6 +17,7 @@ export class ExchangeWebSocketClient {
protected readonly sockets: Map<string, WebSocketClient> = new Map();

private _isStarted = false;
protected reconnectScheduler = new TimeoutScheduler([1000, 5000, 30000, 60000], 60000);

constructor(
protected readonly webSocketApiBaseUrl: string,
Expand Down Expand Up @@ -45,6 +46,8 @@ export class ExchangeWebSocketClient {
this.removeSocket(userId);
});

this.reconnectScheduler.dispose();

this._isStarted = false;
}

Expand Down Expand Up @@ -84,8 +87,8 @@ export class ExchangeWebSocketClient {
};

protected onClosed = (socket: WebSocketClient, _event: CloseEvent) => {
setTimeout(() => {
this.reconnectScheduler.setTimeout(() => {
socket.connect();
}, 1000);
});
};
}
9 changes: 6 additions & 3 deletions src/clients/webSocket/marketDataWebSocketClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from '../../core/index';
import { EventEmitter, TimeoutScheduler } from '../../core/index';
import type { WebSocketResponseDto } from '../dtos';
import { WebSocketClient } from './webSocketClient';

Expand All @@ -7,6 +7,7 @@ export interface MarketDataWebSocketClientEvents {
}

export class MarketDataWebSocketClient {
protected static readonly reconnectTimeouts = [1000, 5000, 3000, 6000];
protected static readonly MARKET_DATA_URL_PATH = '/ws/marketdata';
protected static readonly TOP_OF_BOOK_STREAM = 'topOfBook';
protected static readonly ORDER_BOOK_STREAM = 'orderBook';
Expand All @@ -18,6 +19,7 @@ export class MarketDataWebSocketClient {
protected socket: WebSocketClient;

private _isStarted = false;
protected reconnectScheduler = new TimeoutScheduler([1000, 5000, 30000, 60000], 60000);

constructor(
protected readonly webSocketApiBaseUrl: string
Expand Down Expand Up @@ -49,6 +51,7 @@ export class MarketDataWebSocketClient {
this.socket.events.messageReceived.removeListener(this.onSocketMessageReceived);
this.socket.events.closed.removeListener(this.onSocketClosed);
this.socket.disconnect();
this.reconnectScheduler.dispose();

this._isStarted = false;
}
Expand All @@ -59,10 +62,10 @@ export class MarketDataWebSocketClient {
}

protected onSocketClosed = (socket: WebSocketClient, _event: CloseEvent) => {
setTimeout(async () => {
this.reconnectScheduler.setTimeout(async () => {
await socket.connect();
this.subscribeOnStreams(socket);
}, 1000);
});
};

protected onSocketMessageReceived = (message: WebSocketResponseDto) => {
Expand Down
2 changes: 2 additions & 0 deletions src/clients/webSocket/webSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ export class WebSocketClient {

async connect(): Promise<void> {
this.disconnect();

return new Promise(resolve => {
const protocols = this.authToken ? ['access_token', this.authToken] : undefined;
this._socket = new WebSocket(this.url, protocols);

this.socket.addEventListener('message', this.onMessageReceived);
this.socket.addEventListener('error', this.onError);
this.socket.addEventListener('close', this.onClosed);
Expand Down

0 comments on commit 6fab5f0

Please sign in to comment.