From 76ef0289ffb21689e6e5d858181875e5ae72a13d Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Mon, 29 Jan 2024 08:40:36 +0100 Subject: [PATCH 1/4] Make connectUrl lazy and once initialized --- lib/stomp_config.dart | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/stomp_config.dart b/lib/stomp_config.dart index 3e26785..584f985 100644 --- a/lib/stomp_config.dart +++ b/lib/stomp_config.dart @@ -70,10 +70,13 @@ class StompConfig { /// Callback for debug messages final StompDebugCallback onDebugMessage; + /// The transport url of the WebSocket to connect to String get connectUrl => - useSockJS ? SockJsUtils().generateTransportUrl(url) : url; + _connectUrl ??= useSockJS ? SockJsUtils().generateTransportUrl(url) : url; - const StompConfig({ + String? _connectUrl; + + StompConfig({ required this.url, this.reconnectDelay = const Duration(seconds: 5), this.heartbeatIncoming = const Duration(seconds: 5), From d0fd1b7e611ee351269f40ac8f2f2224f6512260 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:04:41 +0100 Subject: [PATCH 2/4] Add `resetSession` method --- lib/stomp_config.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/stomp_config.dart b/lib/stomp_config.dart index 584f985..3ad0755 100644 --- a/lib/stomp_config.dart +++ b/lib/stomp_config.dart @@ -160,6 +160,9 @@ class StompConfig { ); } + /// Resets the transport URL + void resetSession() => _connectUrl = null; + static void _noOp([_, __]) {} static Future _noOpFuture() => Future.value(); From 586c48bd2d1f33a8a702dc393e2c5dcfc5d0587d Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:05:15 +0100 Subject: [PATCH 3/4] Reset transport (connect) URL on each connect --- lib/stomp_handler.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stomp_handler.dart b/lib/stomp_handler.dart index fa92eca..a821080 100644 --- a/lib/stomp_handler.dart +++ b/lib/stomp_handler.dart @@ -53,7 +53,7 @@ class StompHandler { void start() async { _isActive = true; try { - _channel = await platform.connect(config); + _channel = await platform.connect(config..resetSession()); // It can happen that dispose was called while the future above hasn't completed yet // To prevent lingering connections we need to make sure that we disconnect cleanly if (!_isActive) { From a605ff130de7d6a55e362903c7cb796a38728574 Mon Sep 17 00:00:00 2001 From: Aleksei <130981115+MacDeveloper1@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:07:11 +0100 Subject: [PATCH 4/4] Add `stomp_config_test` --- test/stomp_config_test.dart | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/stomp_config_test.dart diff --git a/test/stomp_config_test.dart b/test/stomp_config_test.dart new file mode 100644 index 0000000..47e44de --- /dev/null +++ b/test/stomp_config_test.dart @@ -0,0 +1,51 @@ +import 'dart:async'; + +import 'package:test/test.dart'; +import 'package:web_socket_channel/web_socket_channel.dart'; + +import 'package:stomp_dart_client/stomp_config.dart'; +import 'package:stomp_dart_client/src/_connect_api.dart' + if (dart.library.html) '../lib/src/_connect_html.dart' + if (dart.library.io) '../lib/src/_connect_io.dart' as platform; + +void main() { + group('StompConfig', () { + test('Generate session URL once per connection', () async { + final config = StompConfig.sockJS( + url: 'http://localhost', + reconnectDelay: Duration(milliseconds: 500), + ); + + final connectUrls = {}; + + void connect() async { + try { + await platform.connect(config..resetSession()); + } on WebSocketChannelException catch (_) { + // Save subsequent calls of `connectUrl`. + connectUrls.addAll({config.connectUrl: config.connectUrl}); + if (connectUrls.length == 1) { + // On 1st connect we expect that the current stored values are equal + expect(connectUrls.entries.first.key, + equals(connectUrls.entries.first.value)); + } else if (connectUrls.length == 2) { + // On 2nd connect we expect that the current stored values are equal + expect(connectUrls.entries.last.key, + equals(connectUrls.entries.last.value)); + // But they are different from the values saved on 1st connect + expect(connectUrls.entries.first.key, + isNot(equals(connectUrls.entries.last.key))); + expect(connectUrls.entries.first.value, + isNot(equals(connectUrls.entries.last.value))); + } + Timer(config.reconnectDelay, connect); + } + } + + connect(); + + // Wait until exit + await Future.delayed(Duration(seconds: 3)); + }); + }); +}