本文介绍了在托管MQTTnet客户端中未触发ApplicationMessageProcessed事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MQTTnet是用于基于MQTT的通信的高性能.NET库.

MQTTnet is a high performance .NET library for MQTT based communication.

这是GitHub链接. https://github.com/chkr1011/MQTTnet .它提供了一个MQTT客户端和一个MQTT服务器(代理).该实现基于 http://mqtt.org/中的文档.

This is GitHub Link.https://github.com/chkr1011/MQTTnet . It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.

这就是我创建managed MQTT客户端的方式.链接在这里 https://github.com/chkr1011/MQTTnet/wiki/ManagedClient

This is how I have created managed MQTT client.Here is the linkhttps://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

推荐答案

此托管客户端正在使用内部线程处理消息.因此,您必须等待几毫秒,才能让线程处理排队的项目.

This managed client is processing the messages using an internal thread. So you have to wait a couple of milliseconds in order to let the thread process queued items.

这篇关于在托管MQTTnet客户端中未触发ApplicationMessageProcessed事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:35