diff --git a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart index 16475743..ea75cd4c 100644 --- a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart +++ b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws2_connection.dart @@ -108,7 +108,8 @@ class MqttServerWs2Connection extends MqttServerWsConnection { events.EventBus? eventBus, List socketOptions, Duration? socketTimeout, - ) : super(eventBus, socketOptions, socketTimeout); + String? websocketPath, + ) : super(eventBus, socketOptions, socketTimeout, websocketPath); /// Initializes a new instance of the MqttWs2Connection class. MqttServerWs2Connection.fromConnect( @@ -117,7 +118,7 @@ class MqttServerWs2Connection extends MqttServerWsConnection { events.EventBus eventBus, List socketOptions, Duration? socketTimeout, - ) : super(eventBus, socketOptions, socketTimeout) { + ) : super(eventBus, socketOptions, socketTimeout, null) { connect(server, port); } @@ -143,6 +144,11 @@ class MqttServerWs2Connection extends MqttServerWsConnection { } uri = uri.replace(port: port); + // Add custom path if specified, otherwise default to / + if (super.websocketPath != null && super.websocketPath!.isNotEmpty) { + uri = uri.replace(path: super.websocketPath); + } + final uriString = uri.toString(); MqttLogger.log( 'MqttWs2Connection::connect - WS URL is $uriString, protocols are $protocols', @@ -222,6 +228,11 @@ class MqttServerWs2Connection extends MqttServerWsConnection { } uri = uri.replace(port: port); + // Add custom path if specified, otherwise default to / + if (super.websocketPath != null && super.websocketPath!.isNotEmpty) { + uri = uri.replace(path: super.websocketPath); + } + final uriString = uri.toString(); MqttLogger.log( 'MqttWs2Connection::connectAuto - WS URL is $uriString, protocols are $protocols', diff --git a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws_connection.dart b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws_connection.dart index 8a977565..657ca8dc 100644 --- a/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws_connection.dart +++ b/lib/src/connectionhandling/server/mqtt_client_mqtt_server_ws_connection.dart @@ -19,11 +19,15 @@ class MqttServerWsConnection extends MqttServerConnection { /// User defined websocket headers Map? headers; + /// User-defined websocket path. + String? websocketPath; + /// Default constructor MqttServerWsConnection( super.eventBus, super.socketOptions, super.socketTimeout, + this.websocketPath, ); /// Initializes a new instance of the MqttConnection class. @@ -61,6 +65,11 @@ class MqttServerWsConnection extends MqttServerConnection { uri = uri.replace(port: port); + // Add custom path if specified, otherwise default to / + if (websocketPath != null && websocketPath!.isNotEmpty) { + uri = uri.replace(path: websocketPath); + } + final uriString = uri.toString(); MqttLogger.log( 'MqttWsConnection::connect - WS URL is $uriString, protocols are $protocols', @@ -124,6 +133,11 @@ class MqttServerWsConnection extends MqttServerConnection { uri = uri.replace(port: port); + // Add custom path if specified, otherwise default to / + if (websocketPath != null && websocketPath!.isNotEmpty) { + uri = uri.replace(path: websocketPath); + } + final uriString = uri.toString(); MqttLogger.log( 'MqttWsConnection::connectAuto - WS URL is $uriString, protocols are $protocols', diff --git a/lib/src/connectionhandling/server/mqtt_client_synchronous_mqtt_server_connection_handler.dart b/lib/src/connectionhandling/server/mqtt_client_synchronous_mqtt_server_connection_handler.dart index c2e69fdd..b4f24ec5 100644 --- a/lib/src/connectionhandling/server/mqtt_client_synchronous_mqtt_server_connection_handler.dart +++ b/lib/src/connectionhandling/server/mqtt_client_synchronous_mqtt_server_connection_handler.dart @@ -18,11 +18,15 @@ class SynchronousMqttServerConnectionHandler required super.socketOptions, required super.socketTimeout, reconnectTimePeriod = 5000, + this.websocketPath, }) : super(maxConnectionAttempts: maxConnectionAttempts) { connectTimer = MqttCancellableAsyncSleep(reconnectTimePeriod); initialiseListeners(); } + /// User-defined websocket path. + String? websocketPath; + /// Synchronously connect to the specific Mqtt Connection. @override Future internalConnect( @@ -56,6 +60,7 @@ class SynchronousMqttServerConnectionHandler clientEventBus, socketOptions, socketTimeout, + websocketPath, ); } else { MqttLogger.log( @@ -66,6 +71,7 @@ class SynchronousMqttServerConnectionHandler clientEventBus, socketOptions, socketTimeout, + websocketPath, ); } diff --git a/lib/src/mqtt_server_client.dart b/lib/src/mqtt_server_client.dart index c3ef8ba5..9667fb3e 100644 --- a/lib/src/mqtt_server_client.dart +++ b/lib/src/mqtt_server_client.dart @@ -56,6 +56,9 @@ class MqttServerClient extends MqttClient { } } + /// User-defined websocket path. + String? websocketPath; + /// Initializes a new instance of the MqttServerClient class using the /// default Mqtt Port. /// The server hostname or URL to connect to @@ -111,6 +114,7 @@ class MqttServerClient extends MqttClient { if (useWebSocket) { connectionHandler.secure = false; connectionHandler.useWebSocket = true; + connectionHandler.websocketPath = websocketPath; connectionHandler.useAlternateWebSocketImplementation = useAlternateWebSocketImplementation; if (connectionHandler.useAlternateWebSocketImplementation) {