An error occurred while trying to connect the client

Viewed 55
public async void OnButtonClicked(object sender, EventArgs e)
{
    try 
    {
        const string topic = "test/test/button";

        var config = new MqttConfiguration { 
            Port = 1883,
            KeepAliveSecs = 60,
            WaitTimeoutSecs = 2,
        };
        var client = await MqttClient.CreateAsync("test.mosquitto.org", config);
        var clientId = "324ascsaf689as97dc67asd69dsa";
        string message = "Hello my name is Max";

        await client.ConnectAsync(new MqttClientCredentials(clientId)); //clientId
        await client.SubscribeAsync(topic, MqttQualityOfService.ExactlyOnce); //QOS 2
        await client.PublishAsync(new MqttApplicationMessage(topic, Encoding.UTF8.GetBytes($"{message}")), MqttQualityOfService.ExactlyOnce); //QOS 2
    }

    catch (Exception ex)
    {
        testLabel.Text = "Exception found: " + ex.Message;
    }
}

I can't connect to the MQTT client but i dont know why...

Error Message:

System.Net.Mqtt.MqttClientException: 'An error occurred while trying to connect the client '

The Whole Exception Object:

System.Net.Mqtt.MqttClientException: The client 324ascsaf689as97dc67asd69dsa has been disconnected while trying to perform the subscription to topic test/test/button at System.Net.Mqtt.Sdk.MqttClientImpl.SubscribeAsync (System.String topicFilter, System.Net.Mqtt.MqttQualityOfService qos) [0x001df] in C:\a\1\s\src\Client\Sdk\MqttClientImpl.cs:180 at System.Net.Mqtt.Sdk.MqttClientImpl.SubscribeAsync (System.String topicFilter, System.Net.Mqtt.MqttQualityOfService qos) [0x003ab] in C:\a\1\s\src\Client\Sdk\MqttClientImpl.cs:203 at App1.MainPage.OnButtonClicked (System.Object sender, System.EventArgs e) [0x00186] in C:\Users\Desktop\App1\App1\App1\MainPage.xaml.cs:65

0 Answers
Related