Not able to connect with broker using MQTTNet library .net core

I need an help, I can't connect with the broker. I'm using MQTTNet library into my api project .net core this is my code:

// GET: api/<SendCommandController>
[HttpGet]
public void Get()
{

    var options = new MqttClientOptionsBuilder()
    .WithTcpServer("broker.hivemq.com", 1883)
    .Build();

    var factory = new MqttFactory();
    var mqttClient = factory.CreateMqttClient();

    mqttClient.ConnectAsync(options, CancellationToken.None);

    var message = new MqttApplicationMessageBuilder()
    .WithTopic("Test/Mqtt")
    .WithPayload("Hello World")
    .WithExactlyOnceQoS()
    .WithRetainFlag()
    .Build();

    mqttClient.PublishAsync(message, CancellationToken.None); 
}

so I follow the tutorial but can't connect to broker hivemq and I can't connect to my personal broker. So, I tested hivemq broker with mqtt.fx and works. Only in the code the return is connected = false.

Any ideas? the error is "the client is not connected"

enter image description here



Read more here: https://stackoverflow.com/questions/67921437/not-able-to-connect-with-broker-using-mqttnet-library-net-core

Content Attribution

This content was originally published by Mr. Developer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: