From 1508c568dadfe844bccea6741a9d6066aebb9389 Mon Sep 17 00:00:00 2001 From: mrsuciu Date: Fri, 25 Oct 2024 11:16:55 +0300 Subject: [PATCH] Update brokerHostName before MqttClientOptionsBuilder uses it's value --- .../Transport/MqttPubSubConnection.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Libraries/Opc.Ua.PubSub/Transport/MqttPubSubConnection.cs b/Libraries/Opc.Ua.PubSub/Transport/MqttPubSubConnection.cs index 17c9deaf6..6013166c1 100644 --- a/Libraries/Opc.Ua.PubSub/Transport/MqttPubSubConnection.cs +++ b/Libraries/Opc.Ua.PubSub/Transport/MqttPubSubConnection.cs @@ -717,6 +717,17 @@ private MqttClientOptions GetMqttClientOptions() return null; } + // Setup data needed also in mqttClientOptionsBuilder + if ((connectionUri.Scheme == Utils.UriSchemeMqtt) || (connectionUri.Scheme == Utils.UriSchemeMqtts)) + { + if (!String.IsNullOrEmpty(connectionUri.Host)) + { + m_brokerHostName = connectionUri.Host; + m_brokerPort = (connectionUri.Port > 0) ? connectionUri.Port : ((connectionUri.Scheme == Utils.UriSchemeMqtt) ? 1883 : 8883); + m_urlScheme = connectionUri.Scheme; + } + } + ITransportProtocolConfiguration transportProtocolConfiguration = new MqttClientProtocolConfiguration(PubSubConnectionConfiguration.ConnectionProperties); @@ -728,6 +739,7 @@ private MqttClientOptions GetMqttClientOptions() .ProtocolVersion; // create uniques client id string clientId = $"ClientId_{new Random().Next():D10}"; + // MQTTS mqttConnection. if (connectionUri.Scheme == Utils.UriSchemeMqtts) { @@ -794,6 +806,8 @@ private MqttClientOptions GetMqttClientOptions() // Set user credentials. if (mqttProtocolConfiguration.UseCredentials) { + // Following Password usage in both cases is correct since it is the Password position + // to be taken into account for the UserName to be read properly mqttClientOptionsBuilder.WithCredentials( new System.Net.NetworkCredential(string.Empty, mqttProtocolConfiguration.UserName) .Password,