From 3907ac5961f044227fac8c62316112acc0d6ede6 Mon Sep 17 00:00:00 2001 From: Eddort Date: Wed, 29 Mar 2023 17:58:29 +0400 Subject: [PATCH] fix: stomp nest wrapper --- src/transport/stomp/stomp.client.ts | 4 +++- src/transport/stomp/stomp.interface.ts | 3 ++- src/transport/stomp/stomp.spec.ts | 18 ++++++++++++++---- src/transport/transport.constants.ts | 7 +++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/transport/stomp/stomp.client.ts b/src/transport/stomp/stomp.client.ts index 75e45b62..bf8e6897 100644 --- a/src/transport/stomp/stomp.client.ts +++ b/src/transport/stomp/stomp.client.ts @@ -163,9 +163,11 @@ export default class StompClient { public async connect(headers = {}, timeout = 10000) { await this._connect(timeout); + const { maxWaitSocketSession } = this.options; + headers['host'] = '/'; headers['accept-version'] = VERSIONS; - headers['heart-beat'] = '100000,100000'; + headers['heart-beat'] = `${maxWaitSocketSession},${maxWaitSocketSession}`; if (this.login != null) { headers['login'] = this.login; diff --git a/src/transport/stomp/stomp.interface.ts b/src/transport/stomp/stomp.interface.ts index 2d92716e..776a6789 100644 --- a/src/transport/stomp/stomp.interface.ts +++ b/src/transport/stomp/stomp.interface.ts @@ -10,6 +10,7 @@ export type GetWebSocket = (url: string) => WebSocket | WebSocketMock; export type StompOptions = { reconnectTimeout: number; reconnectAttempts: number; + maxWaitSocketSession: number; }; export interface StompDependencies { @@ -18,7 +19,7 @@ export interface StompDependencies { passcode: string | null; connectCallback: ConnectCallback; errorCallback: ErrorCallback; - getWebSocket: GetWebSocket; + getWebSocket?: GetWebSocket; logger?: LoggerService; options: StompOptions; } diff --git a/src/transport/stomp/stomp.spec.ts b/src/transport/stomp/stomp.spec.ts index 21e9c4f6..86cf8ad5 100644 --- a/src/transport/stomp/stomp.spec.ts +++ b/src/transport/stomp/stomp.spec.ts @@ -1,8 +1,15 @@ import StompClient from './stomp.client'; +import { StompOptions } from './stomp.interface'; import { WebSocketMock } from './stomp.mock'; const wait = (ms = 100) => new Promise((resolve) => setTimeout(resolve, ms)); +const DEFAULT_STOMP_OPTIONS: StompOptions = { + reconnectAttempts: 1, + reconnectTimeout: 10, + maxWaitSocketSession: 10_000, +}; + describe('StompClient', () => { describe('regular cases', () => { let server: WebSocketMock; @@ -23,10 +30,7 @@ describe('StompClient', () => { server = new WebSocketMock(); return server; }, - options: { - reconnectAttempts: 1, - reconnectTimeout: 10, - }, + options: DEFAULT_STOMP_OPTIONS, }); await stompClient.connect(); @@ -109,6 +113,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, reconnectAttempts, reconnectTimeout: 10, }, @@ -153,6 +158,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, reconnectAttempts, reconnectTimeout: 10, }, @@ -197,6 +203,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, reconnectAttempts, reconnectTimeout: 10, }, @@ -245,6 +252,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, get reconnectAttempts() { realAttempts += 1; return reconnectAttempts; @@ -291,6 +299,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, get reconnectAttempts() { realAttempts += 1; return reconnectAttempts; @@ -337,6 +346,7 @@ describe('StompClient', () => { return server; }, options: { + maxWaitSocketSession: 10_000, get reconnectAttempts() { realAttempts += 1; return reconnectAttempts; diff --git a/src/transport/transport.constants.ts b/src/transport/transport.constants.ts index f0b730d8..68de8475 100644 --- a/src/transport/transport.constants.ts +++ b/src/transport/transport.constants.ts @@ -1,2 +1,9 @@ +import { StompOptions } from './stomp/stomp.interface'; + export const KAFKA_LOG_PREFIX = 'Kafka'; export const RABBIT_LOG_PREFIX = 'RabbitMQ'; +export const STOMP_OPTIONS: StompOptions = { + reconnectAttempts: 2, + reconnectTimeout: 5_000, + maxWaitSocketSession: 100_000, +};