dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.51k stars 1.07k forks source link

MQTTnet.Exception.MqttCommunicationTimeOutException #727

Closed NowUp closed 4 years ago

NowUp commented 5 years ago

I met a MqttCommunicationTimeOutException, but I'm not sure what the problem, hope to be able to get help. Version 3.0.5 is used.

This is the exception information:

MQTTnet.Exceptions.MqttCommunicationTimedOutException: Exception of type 'MQTTnet.Exceptions.MqttCommunicationTimedOutException' was thrown.
   at MQTTnet.PacketDispatcher.MqttPacketAwaiter`1.WaitOneAsync(TimeSpan timeout)
   at MQTTnet.Client.MqttClient.SendAndReceiveAsync[TResponsePacket](MqttBasePacket requestPacket, CancellationToken cancellationToken)
   at MQTTnet.Client.MqttClient.PublishAtLeastOnceAsync(MqttPublishPacket publishPacket, CancellationToken cancellationToken)

This is considered critical code:

MqttClient.UseApplicationMessageReceivedHandler(e =>
{
    MqttClient_ApplicationMessageReceived(e);
});

private void MqttClient_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e)
{
    Console.WriteLine("### 从服务端接收的消息 ###");
    Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
    Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
    Console.WriteLine();

    // After processing, the theme is republished
    Event_MQTTMessageReceived?.Invoke(e);
}

public void Publish(string topic, string Data)
{
    if (MqttClient != null && MqttClient.IsConnected)
    {
        var _Message = new MqttApplicationMessageBuilder();
        _Message.WithTopic(topic);
        _Message.WithPayload(Data);
        _Message.WithAtMostOnceQoS();
        _Message.WithRetainFlag(false);

        MqttClient.PublishAsync(_Message.Build());

        Console.WriteLine("### 从本地发送的消息 ###");
        Console.WriteLine($"+ Topic = {topic}");
        Console.WriteLine($"+ Payload = {Data}");
        Console.WriteLine();
    }
}
SeppPenner commented 5 years ago

Which platform does your client run on and to which server do you connect?

And what is

// After processing, the theme is republished
Event_MQTTMessageReceived?.Invoke(e);

supposed to do?

NowUp commented 5 years ago

The client: Windows Server 2019 Datacenter; The Server: Linx CentOS 6.8 ; MQtt Agency: Apache apollo;

Event_MQTTMessageReceived?.Invoke(e); This is when a message is received, processed in another method, and then the topic is published.

SeppPenner commented 5 years ago

The client: Windows Server 2019 Datacenter; The Server: Linx CentOS 6.8 ; MQtt Agency: Apache apollo;

Are you using .NetCore, .NetFramework, AspNetCore or UWP? I assume either one of the first 3?

As far as I remember correctly, there was an issue with handling publish events in the ApplicationMessageReceivedHandler. I'm not able to find it yet.

@chkr1011 Can you help here?

NowUp commented 5 years ago

Is the.net Core 2.1. I wasn't sure what the problem was, so I put out code that I thought might be important, hoping it would help someone who could fix it.

chkr1011 commented 5 years ago

@NowUp Please remove _EventMQTTMessageReceived?.Invoke(e); for testing and make sure you are awaiting the call of PublishAsync. I can see no async or Task. So I assume the issue is related to that.

chkr1011 commented 5 years ago

I assume this is fixed. If not please reopen this ticket.

xirami commented 5 years ago

Same issue on my end, no subscribe channel at all.

jingsong-liu commented 4 years ago

Same issue on my end. Windwos 10, version 1909 Mqttnet Version 3.0.12 Repetition is Occasional。

SeppPenner commented 4 years ago

@chkr1011 I will re-open this.

jingsong-liu commented 4 years ago

My question is solved. The cause is that client has possibility of unsubscribing topic after disconnected.