From 1a2dc04b469724bb4938d1d71422bd4fca270e8f Mon Sep 17 00:00:00 2001 From: Juergen Werner Date: Fri, 13 Feb 2026 13:53:14 +0100 Subject: [PATCH] Remove MqttEnvironment.isWebClient and implement proper fix for `issue168` The original fix was more of a hack involving multiple buffer copies and global variable to guide code paths. The new fix is only one line change in MqttBrowserConnection. The internal buffer size of Uint8Buffer may be larger than Uint8Buffer.length and that was the culprit of the original issue. The rest of the changes are just reverting the original fix and cleaning up. --- lib/mqtt5_client.dart | 2 -- .../browser/mqtt_browser_connection.dart | 2 +- lib/src/mqtt_browser_client.dart | 1 - lib/src/mqtt_environment.dart | 14 -------------- lib/src/mqtt_server_client.dart | 2 +- lib/src/utility/mqtt_byte_buffer.dart | 15 +++------------ ...t_client_mosquitto_ws_browser_test_manual.dart | 2 -- test/mqtt_client_connection_unsecure.dart | 2 -- 8 files changed, 5 insertions(+), 35 deletions(-) delete mode 100644 lib/src/mqtt_environment.dart diff --git a/lib/mqtt5_client.dart b/lib/mqtt5_client.dart index eff7ecb..6b65a28 100644 --- a/lib/mqtt5_client.dart +++ b/lib/mqtt5_client.dart @@ -21,8 +21,6 @@ part 'src/mqtt_constants.dart'; part 'src/mqtt_protocol.dart'; -part 'src/mqtt_environment.dart'; - part 'src/mqtt_event.dart'; part 'src/mqtt_event_bus.dart'; diff --git a/lib/src/connectionhandling/browser/mqtt_browser_connection.dart b/lib/src/connectionhandling/browser/mqtt_browser_connection.dart index 0bcd8ee..165518b 100644 --- a/lib/src/connectionhandling/browser/mqtt_browser_connection.dart +++ b/lib/src/connectionhandling/browser/mqtt_browser_connection.dart @@ -129,7 +129,7 @@ abstract class MqttBrowserConnection extends MqttConnectionBase { final length = message.length; final messageBytes = message.read(length); var buffer = messageBytes.buffer; - var bData = ByteData.view(buffer); + var bData = ByteData.view(buffer, 0, length); wsClient.send(bData.jsify()!); } diff --git a/lib/src/mqtt_browser_client.dart b/lib/src/mqtt_browser_client.dart index 2914fbb..5e8e8f4 100644 --- a/lib/src/mqtt_browser_client.dart +++ b/lib/src/mqtt_browser_client.dart @@ -44,7 +44,6 @@ class MqttBrowserClient extends MqttClient { String? username, String? password, ]) async { - MqttEnvironment.isWebClient = true; instantiationCorrect = true; clientEventBus = MqttEventBus.fromEventBus(events.EventBus()); clientEventBus?.on().listen( diff --git a/lib/src/mqtt_environment.dart b/lib/src/mqtt_environment.dart deleted file mode 100644 index abca47c..0000000 --- a/lib/src/mqtt_environment.dart +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Package : mqtt5_client - * Author : S. Hamblett - * Date : 09/06/2025 - * Copyright : S.Hamblett - */ - -part of '../mqtt5_client.dart'; - -/// Client Environment -class MqttEnvironment { - /// Browser or server client - static bool isWebClient = false; -} diff --git a/lib/src/mqtt_server_client.dart b/lib/src/mqtt_server_client.dart index a9cd360..71451ac 100644 --- a/lib/src/mqtt_server_client.dart +++ b/lib/src/mqtt_server_client.dart @@ -51,6 +51,7 @@ class MqttServerClient extends MqttClient { /// /// Minimum value is 1000ms. int? get socketTimeout => _socketTimeout; + set socketTimeout(int? period) { if (period != null && period >= MqttConstants.minimumSocketTimeoutPeriod) { _socketTimeout = period; @@ -90,7 +91,6 @@ class MqttServerClient extends MqttClient { String? username, String? password, ]) async { - MqttEnvironment.isWebClient = false; instantiationCorrect = true; clientEventBus = MqttEventBus.fromEventBus(events.EventBus()); clientEventBus?.on().listen( diff --git a/lib/src/utility/mqtt_byte_buffer.dart b/lib/src/utility/mqtt_byte_buffer.dart index e9ba32d..fd36258 100644 --- a/lib/src/utility/mqtt_byte_buffer.dart +++ b/lib/src/utility/mqtt_byte_buffer.dart @@ -143,18 +143,9 @@ class MqttByteBuffer { 'length $length, count $count, position $_position, buffer $buffer', ); } - if (MqttEnvironment.isWebClient) { - final tmp = typed.Uint8Buffer(); - tmp.addAll(buffer!.getRange(_position, _position + count)); - _position += count; - final tmp2 = typed.Uint8Buffer(); - tmp2.addAll(tmp); - return tmp2; - } else { - _position += count; - return typed.Uint8Buffer() - ..addAll(buffer!.getRange(_position - count, _position)); - } + final start = _position; + _position += count; + return typed.Uint8Buffer()..addAll(buffer!.getRange(start, _position)); } /// Reads a sequence of bytes from the current diff --git a/test/manual/mqtt_client_mosquitto_ws_browser_test_manual.dart b/test/manual/mqtt_client_mosquitto_ws_browser_test_manual.dart index 3edbfa3..d1a0887 100644 --- a/test/manual/mqtt_client_mosquitto_ws_browser_test_manual.dart +++ b/test/manual/mqtt_client_mosquitto_ws_browser_test_manual.dart @@ -43,9 +43,7 @@ void main() { client.connectionMessage = connMess; var ok = true; try { - expect(MqttEnvironment.isWebClient, isFalse); await client.connect(); - expect(MqttEnvironment.isWebClient, isTrue); var connectionOK = false; if (client.connectionStatus!.state == MqttConnectionState.connected) { print('Browser client connected locally'); diff --git a/test/mqtt_client_connection_unsecure.dart b/test/mqtt_client_connection_unsecure.dart index 74e2d1a..674c2b0 100644 --- a/test/mqtt_client_connection_unsecure.dart +++ b/test/mqtt_client_connection_unsecure.dart @@ -216,13 +216,11 @@ void main() { ); broker.setMessageHandler = messageHandler; ch.onConnected = connectCb; - expect(MqttEnvironment.isWebClient, isFalse); await ch.connect( mockBrokerAddress, mockBrokerPort, MqttConnectMessage().withClientIdentifier(testClientId), ); - expect(MqttEnvironment.isWebClient, isFalse); expect(ch.connectionStatus.state, MqttConnectionState.connected); expect(ch.connectionStatus.reasonCode, MqttConnectReasonCode.success); expect(connectCbCalled, isTrue);