I’m connecting via wss/8084
from my Angular 14 web client, however I’m no longer able to connect via the .Net client. I keep getting WebSocket Connection Exceptions in C#.
Exception thrown: 'MQTTnet.Adapter.MqttConnectingFailedException' in MQTTnet.dll
I’m checking the .Net samples here: MQTT-Client-Examples/Program.cs at master · emqx/MQTT-Client-Examples · GitHub
public async void StartPublisher()
{
var mqttFactory = new MqttFactory();
var tlsOptions = new MqttClientTlsOptions
{
UseTls = MqttUseTls,
IgnoreCertificateChainErrors = true,
IgnoreCertificateRevocationErrors = true,
AllowUntrustedCertificates = true
};
var managedClientOptions = WsMqttClientOptions();
this.managedMqttClientPublisher = mqttFactory.CreateManagedMqttClient();
await this.managedMqttClientPublisher.StartAsync(managedClientOptions);
Publish(defaultMessage, topicPubToHar); // *** PUBLISH ***
}
public ManagedMqttClientOptions WsMqttClientOptions()
{
// secure wss url = "a37d0ae6.ala.us-east-1.emqxsl.com:8084/mqtt"
var url = $"{mqttBrokerIpAddress}:{mqttPort}/mqtt";
var caFile = "broker.emqx.io-ca.crt";
// certFilePath = "C:\\bob\\dev\\mqttNotifications\\mqttNotificationsService\\bin\\x64\\Debug\\broker.emqx.io-ca.crt";
string path = Path.GetDirectoryName(
Assembly.GetAssembly(typeof(MqttService)).CodeBase);
var certFilePath = Path.Combine(path, caFile).Remove(0, 6);
var clientOptionsBldr = new MqttClientOptionsBuilder()
.WithWebSocketServer(url)
.WithCredentials(mqttClientUser, mqttClientPswd)
.WithClientId(clientId)
.WithCleanSession()
.WithCredentials(mqttClientUser, mqttClientPswd)
.WithTls(
new MqttClientOptionsBuilderTlsParameters()
{
UseTls = true,
SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
Certificates = new List<X509Certificate>()
{
// Download from https://www.emqx.com/en/mqtt/public-mqtt5-broker
X509Certificate.CreateFromCertFile(certFilePath)
}
}
);
ManagedMqttClientOptions managedClientOptions =
new ManagedMqttClientOptionsBuilder()
.WithClientOptions(clientOptionsBldr)
.Build();
return managedClientOptions;
}
// PUBLISH MESSAGE
public async void Publish(string message, string topic)
{
message += " " + DateTime.Now.ToString();
var payload = Encoding.UTF8.GetBytes(message);
var send = new MqttApplicationMessageBuilder()
.WithTopic(topic)
.WithPayload(payload)
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
.WithRetainFlag(false)
.Build();
if (this.managedMqttClientPublisher != null)
{
await this.managedMqttClientPublisher.EnqueueAsync(send);
}
}