MQTTnet
是用于基于MQTT的通信的高性能.NET库。
这是GitHub链接。 https://github.com/chkr1011/MQTTnet。它提供了一个MQTT客户端和一个MQTT服务器(代理)。该实现基于http://mqtt.org/中的文档。
这就是我创建managed
MQTT客户端的方式。
链接在这里
https://github.com/chkr1011/MQTTnet/wiki/ManagedClient
// Setup and start a managed MQTT client.
var options = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(new MqttClientOptionsBuilder()
.WithClientId("Client1")
.WithTcpServer("broker.hivemq.com")
.WithTls().Build())
.Build();
this.mqttClient = new MqttFactory().CreateManagedMqttClient(new MqttNetLogger("IDMQTTManagedPublisher"));
await this.mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("RequestTopic").Build());
SubscribeToApplicationMessageReceived();
await this.mqttClient.StartAsync(options);
订阅ApplicationMessageProcessed
事件
private void SubscribeToApplicationMessageProcessed()
{
this.mqttClient.ApplicationMessageProcessed += (s, e) =>
{
};
}
消息发送代码
var messagePayload = new MqttApplicationMessageBuilder()
.WithTopic("RequestTopic")
.WithPayload(message)
.WithExactlyOnceQoS()
.WithRetainFlag()
.Build();
await mqttClient.PublishAsync(messagePayload);
但是ApplicationMessageProcessed
中没有触发managed MQTTnet client
事件
答案 0 :(得分:0)
此受管客户端正在使用内部线程处理消息。因此,您必须等待几毫秒,才能让线程处理排队的项目。