0

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}.");
}
  • 1
    Please edit the question to include your "local mosquitto"'s config file and log file from when you attempted to connect. How is your "local mosquitto" installed? Is it on the same computer that you're running this code on? If it's not, you need to use the correct name or IP address for the computer mosquitto is installed on and not "localhost". – romkey Sep 06 '22 at 21:30
  • 1
    Note that I believe `.WithKeepAlivePeriod(new TimeSpan(1))` will set the keepalive period to 100 nanoseconds; is that your intention (something like `TimeSpan.FromSeconds(60)` may be more appropriate). – Brits Sep 06 '22 at 23:30
  • Config file is default. I changed only log_dest and password_file. Log file is empty. I'm using client on local machine. – Kacper Wojnar Sep 07 '22 at 08:04
  • Please [edit](https://stackoverflow.com/posts/73627744/edit) the question to actually show show the config file, no logs implies that there is an error in the config file. – hardillb Sep 07 '22 at 08:42
  • Added. 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 – Kacper Wojnar Sep 07 '22 at 09:06
  • Try wraping the file paths in quotes because they have spaces. – hardillb Sep 07 '22 at 10:29
  • this solved the problem, the quotation marks alone did not allow me to start the service, but changing the file location to without spaces helped, Thanks! – Kacper Wojnar Sep 07 '22 at 12:01
  • Just FYI - the "Program Files" folder is generally protected so only admin users (who have also satisfied [UAC](https://learn.microsoft.com/en-us/windows/security/identity-protection/user-account-control/how-user-account-control-works)) can write there. This means that its not a good place to store logs etc. – Brits Sep 07 '22 at 19:59

0 Answers0