Mqtt connection timeout - localhost mosquitto

Viewed 110

I'm trying to connect to my local mosquitto by this code, but always get this error:

Error while authenticating. The operation has timed out. 

Any idea what I'm doing wrong?

I manage to connect to the server test.mosquitto.org by this code.

Also I manage to connecto to localhost via MQTT Explorer.

However, connecting using mqttnet is impossible

mosquitto.conf:

Config file is default. I only have these parameters set:

allow_anonymous true
password_file C:\Program Files\mosquitto\passwd
tls_version tlsv1.2
log_dest file C:\Program Files\mosquitto\log\mosquitto.log

Log file is empty.

   class Program
{
    static async Task Main()
    {
        var mqttClientId = "MyClientId";             // Unique ClientId or pass a GUID as string for something random
        var mqttBrokerAddress = "localhost";         // hostname or IP address of your MQTT broker
        var mqttBrokerUsername = "guest";       // Broker Auth username if using auth
        var mqttBrokerPassword = "guest";       // Broker Auth password if using auth
        var topic = "topic";                 // topic to subscribe to
        var mqttClient = new MqttFactory().CreateManagedMqttClient();
        var mqttClientOptions = new ManagedMqttClientOptionsBuilder()
                    .WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
                    .WithClientOptions(new MqttClientOptionsBuilder()
                        .WithTcpServer(mqttBrokerAddress, 1883)
                        .WithCredentials(mqttBrokerUsername, mqttBrokerPassword)    // Remove this line if no auth
                        .WithCleanSession().WithKeepAlivePeriod(new TimeSpan(1))//.WithTls(tlsOptions)
                        .Build()
                    )
                    .Build();

        mqttClient.ApplicationMessageReceivedAsync += async e => MqttOnNewMessage(e);
        mqttClient.ConnectedAsync += async e => MqttOnConnected(e);
        mqttClient.DisconnectedAsync += async e => MqttOnDisconnected(e);
        mqttClient.SynchronizingSubscriptionsFailedAsync += async e => test(e);
        mqttClient.ConnectingFailedAsync += async e => test2(e);
        var aaa = new List<MqttTopicFilter>();
        var bbb = new MqttTopicFilterBuilder().WithTopic(topic).Build();
        aaa.Add(bbb);
        try
        {
            await mqttClient.SubscribeAsync(aaa);
            await mqttClient.StartAsync(mqttClientOptions);
        }
        catch (Exception ex)
        {

            throw;
        }
        Console.ReadLine();
    }

    private static void test2(ConnectingFailedEventArgs e)
    {
        Console.WriteLine();
    }

    private static void test(ManagedProcessFailedEventArgs e)
    {
        Console.WriteLine();
    }

    private static void MqttOnNewMessage(MqttApplicationMessageReceivedEventArgs e)
    {
        // Do something with each incoming message from the topic

        Console.WriteLine($"MQTT Client: OnNewMessage Topic: {e.ApplicationMessage.Topic} / Message: {e.ApplicationMessage.Payload}");
    }

    private static void MqttOnConnected(MqttClientConnectedEventArgs e) => Console.WriteLine($"MQTT Client: Connected with result: {e.ConnectResult.ResultCode}");

    private static void MqttOnDisconnected(MqttClientDisconnectedEventArgs e) => Console.WriteLine($"MQTT Client: Broker connection lost with reason: {e.Reason}.");
}
0 Answers
Related