I'm using MQTTnet library to connect to my MQTT server that needs a server certificate. The client one is not needed.
I already installed the certificate inside my PC as i found in other post and created the .pfx file to create the certificate but the program doesn't give me any error..it simply doesn't connect to the topic.
This is my example code
//Create a new MQTT client
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
var caCert = new X509Certificate(@"C:caserverroot.pfx", "mypsw");
var url = "mymqtt.com";
var username = "user";
var psw = "user";
var port = 8885;
var options = new MqttClientOptionsBuilder()
.WithClientId(Guid.NewGuid().ToString())
.WithTcpServer(url, port)
.WithCredentials(username, psw)
.WithTls(new MqttClientOptionsBuilderTlsParameters()
{
AllowUntrustedCertificates = true,
UseTls = true,
Certificates = new List<byte[]> { new X509Certificate2(caCert).Export(X509ContentType.Cert) },
CertificateValidationCallback = delegate { return true; },
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false
})
.WithCleanSession()
.WithProtocolVersion(MQTTnet.Serializer.MqttProtocolVersion.V311)
.Build();
// Connecting
var result = await mqttClient.ConnectAsync(options);
// Subscribe to a topic
mqttClient.Connected += async (s, e) =>
{
Console.WriteLine("### CONNECTED WITH SERVER ###");
await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("/mytopic").Build());
Console.WriteLine("### SUBSCRIBED ###");
};
With all the orther events that i found here: https://github.com/chkr1011/MQTTnet/wiki/Client
Any of you had experience about this library? How to debug it and find the error?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…