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.39k stars 1.05k forks source link

My client receive Disconnected when I set mqqtv5 to connect emqx bloker #1912

Open iswing opened 7 months ago

iswing commented 7 months ago

I did following tests

Here is my Code


 _mqttClientOptions = new MqttClientOptionsBuilder()
            .WithProtocolVersion(MqttProtocolVersion.V500)
            .WithCredentials("my_user", "testpsw")
            .WithTcpServer("exmq.xxx.cn", 1883).Build();

        _client.ApplicationMessageReceivedAsync += ProcessEventAsync;
        _client.DisconnectedAsync += DisconnectedAsync;

        _ = Task.Run(
            async () =>
            {
                // User proper cancellation and no while(true).
                while (true)
                {
                    try
                    {

                        // This code will also do the very first connect! So no call to _ConnectAsync_ is required in the first place.
                        if (await _client.TryPingAsync()) continue;
                        _logger.LogInformation("Try to connect the blocker");
                        await _client.ConnectAsync(_mqttClientOptions, CancellationToken.None);

                        SubscribeHandlers(_abpMqttEventBusOptions.Handlers);
                        _logger.LogInformation("Connected");
                    }
                    catch (Exception e)
                    {
                        _logger.LogException(e);
                    }
                    finally
                    {
                        // Check the connection state every 5 seconds and perform a reconnect if required.
                        await Task.Delay(TimeSpan.FromSeconds(5));
                    }
                }
            });
    }

    private Task DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
    {
        _logger.LogError(arg.Exception, $"{arg.Reason.ToString()}({(int) arg.Reason})");
        return Task.CompletedTask;
    }

2024-01-19 14:25:18[INF][MqttEventBus] - Connected 2024-01-19 14:25:18[ERR][MqttEventBus] - ProtocolError(130)

chkr1011 commented 7 months ago

Do you have access to the EMQX logs and see any errors or something of interest? I tested with the public broker at "broker.emqx.io" and it can connect with protocol version 3.1.1 and also 5.0.0.

zhangzw218 commented 3 weeks ago

Do you have access to the EMQX logs and see any errors or something of interest? I tested with the public broker at "broker.emqx.io" and it can connect with protocol version 3.1.1 and also 5.0.0.

I had the same problem, subscribing to a shared theme, and I got an error sub "$share/service1/online/pub" // ProtocolError sub "online/pub" //succeed