move timers to keyed store
This commit is contained in:
@@ -76,9 +76,6 @@ const getLinearWsKeyForTopic = (topic: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class WebsocketClient extends EventEmitter {
|
export class WebsocketClient extends EventEmitter {
|
||||||
private activePingTimer?: NodeJS.Timeout | undefined;
|
|
||||||
private activePongTimer?: NodeJS.Timeout | undefined;
|
|
||||||
|
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
private client: InverseClient | LinearClient;
|
private client: InverseClient | LinearClient;
|
||||||
private options: WebsocketClientOptions;
|
private options: WebsocketClientOptions;
|
||||||
@@ -90,8 +87,6 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
|
|
||||||
this.logger = logger || DefaultLogger;
|
this.logger = logger || DefaultLogger;
|
||||||
this.wsStore = new WsStore(this.logger);
|
this.wsStore = new WsStore(this.logger);
|
||||||
this.activePingTimer = undefined;
|
|
||||||
this.activePongTimer = undefined;
|
|
||||||
|
|
||||||
this.options = {
|
this.options = {
|
||||||
livenet: false,
|
livenet: false,
|
||||||
@@ -252,12 +247,12 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ping(wsKey: string = defaultWsKey) {
|
private ping(wsKey: string = defaultWsKey) {
|
||||||
this.clearPongTimer();
|
this.clearPongTimer(wsKey);
|
||||||
|
|
||||||
this.logger.silly('Sending ping', loggerCategory);
|
this.logger.silly('Sending ping', loggerCategory);
|
||||||
this.tryWsSend(wsKey, JSON.stringify({ op: 'ping' }));
|
this.tryWsSend(wsKey, JSON.stringify({ op: 'ping' }));
|
||||||
|
|
||||||
this.activePongTimer = setTimeout(() => {
|
this.wsStore.get(wsKey, true)!.activePongTimer = setTimeout(() => {
|
||||||
this.logger.info('Pong timeout - closing socket to reconnect', loggerCategory);
|
this.logger.info('Pong timeout - closing socket to reconnect', loggerCategory);
|
||||||
this.getWs(wsKey)?.close();
|
this.getWs(wsKey)?.close();
|
||||||
}, this.options.pongTimeout);
|
}, this.options.pongTimeout);
|
||||||
@@ -269,18 +264,20 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send a ping at intervals
|
// Send a ping at intervals
|
||||||
private clearPingTimer(wsKey: string = defaultWsKey) {
|
private clearPingTimer(wsKey: string) {
|
||||||
if (this.activePingTimer) {
|
const wsState = this.wsStore.get(wsKey);
|
||||||
clearInterval(this.activePingTimer);
|
if (wsState?.activePingTimer) {
|
||||||
this.activePingTimer = undefined;
|
clearInterval(wsState.activePingTimer);
|
||||||
|
wsState.activePingTimer = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expect a pong within a time limit
|
// Expect a pong within a time limit
|
||||||
private clearPongTimer(wsKey: string = defaultWsKey) {
|
private clearPongTimer(wsKey: string) {
|
||||||
if (this.activePongTimer) {
|
const wsState = this.wsStore.get(wsKey);
|
||||||
clearTimeout(this.activePongTimer);
|
if (wsState?.activePongTimer) {
|
||||||
this.activePongTimer = undefined;
|
clearInterval(wsState.activePongTimer);
|
||||||
|
wsState.activePongTimer = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,14 +339,17 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
this.requestSubscribeTopics(wsKey, [...this.wsStore.getTopics(wsKey)])
|
this.requestSubscribeTopics(wsKey, [...this.wsStore.getTopics(wsKey)])
|
||||||
);
|
);
|
||||||
|
|
||||||
this.activePingTimer = setInterval(this.ping.bind(this), this.options.pingInterval);
|
this.wsStore.get(wsKey, true)!.activePingTimer = setInterval(
|
||||||
|
this.ping.bind(this),
|
||||||
|
this.options.pingInterval
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private onWsMessage(event, wsKey?: string) {
|
private onWsMessage(event, wsKey: string) {
|
||||||
const msg = JSON.parse(event && event.data || event);
|
const msg = JSON.parse(event && event.data || event);
|
||||||
|
|
||||||
if ('success' in msg) {
|
if ('success' in msg) {
|
||||||
this.onWsMessageResponse(msg);
|
this.onWsMessageResponse(msg, wsKey);
|
||||||
} else if (msg.topic) {
|
} else if (msg.topic) {
|
||||||
this.onWsMessageUpdate(msg);
|
this.onWsMessageUpdate(msg);
|
||||||
} else {
|
} else {
|
||||||
@@ -368,7 +368,7 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
this.logger.info('Websocket connection closed', loggerCategory);
|
this.logger.info('Websocket connection closed', loggerCategory);
|
||||||
|
|
||||||
if (this.wsStore.getConnectionState(wsKey) !== READY_STATE_CLOSING) {
|
if (this.wsStore.getConnectionState(wsKey) !== READY_STATE_CLOSING) {
|
||||||
this.reconnectWithDelay(this.options.reconnectTimeout!);
|
this.reconnectWithDelay(wsKey, this.options.reconnectTimeout!);
|
||||||
this.emit('reconnect');
|
this.emit('reconnect');
|
||||||
} else {
|
} else {
|
||||||
this.setWsState(wsKey, READY_STATE_INITIAL);
|
this.setWsState(wsKey, READY_STATE_INITIAL);
|
||||||
@@ -376,10 +376,10 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private onWsMessageResponse(response: any) {
|
private onWsMessageResponse(response: any, wsKey: string) {
|
||||||
if (isWsPong(response)) {
|
if (isWsPong(response)) {
|
||||||
this.logger.silly('Received pong', loggerCategory);
|
this.logger.silly('Received pong', loggerCategory);
|
||||||
this.clearPongTimer();
|
this.clearPongTimer(wsKey);
|
||||||
} else {
|
} else {
|
||||||
this.emit('response', response);
|
this.emit('response', response);
|
||||||
}
|
}
|
||||||
@@ -402,7 +402,7 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
return this.options.wsUrl;
|
return this.options.wsUrl;
|
||||||
}
|
}
|
||||||
if (this.options.linear){
|
if (this.options.linear){
|
||||||
return linearEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
return linearEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
}
|
}
|
||||||
return inverseEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
return inverseEndpoints[this.options.livenet ? 'livenet' : 'testnet'];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user