在托管MQTTnet客户端中未触发ApplicationMessageProcessed事件

时间:2018-07-10 13:29:47

标签: c# mqtt

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事件

1 个答案:

答案 0 :(得分:0)

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