首页 > 解决方案 > MQTTnet TLS 连接

问题描述

经过一番研究,我终于得到了一段工作代码,可以将 MQTT 消息发送到测试 mosquitto MQTT 服务器

var mqttClient = factory.CreateMqttClient();

var options = new MqttClientOptionsBuilder()
                 .WithTcpServer("test.mosquitto.org", 1883)
                 .WithClientId("client123")
                 .Build();

await mqttClient.ConnectAsync(options);

Main account = new Account
{
    Email = "test@example.com",
    Active = true,
    CreatedDate = new DateTime(2013, 1, 20, 0, 0, 0, DateTimeKind.Utc),
    Roles = new List<string>
    {
         "User",
         "Admin"
    }
};

string json = JsonConvert.SerializeObject(account, Formatting.Indented);

var message = new MqttApplicationMessageBuilder()
                 .WithTopic("MyTopic")
                 .WithPayload(json)
                 .WithExactlyOnceQoS()
                 .WithRetainFlag()
                 .Build();

await mqttClient.PublishAsync(message);
Console.WriteLine("press return to carry on");
Console.ReadLine();

我的下一步是使用 TLS 订阅和发布到 AWS IoT 上的主题。

我知道我必须使用(在 MqttClientOptionsBuilder 中)

.WithTls()

但我不确定如何指定使用哪个证书进行身份验证

任何帮助,将不胜感激

标签: c#.netxamarinmqttaws-iot

解决方案


推荐阅读