简体   繁体   中英

MQTTnet Connection Issue with HiveMQ Cloud

I am new to the MQTT world and I am trying to create a.Net 5.0 application that connects to a HiveMQ Cloud Broker.

I have created a free broker and I am able to connect to it with HiveMQ Websocket Client.

Here is a screenshot of my host.

在此处输入图像描述

I have created MQTT credentials for the host and I am able to connect over the sample client. Here is a screenshot of that client.

在此处输入图像描述

This works, I can publish and subscribe to the message queue.

However, now I am trying to translate this to c# and I am not able to connect. I am starting with this example project: https://github.com/rafiulgits/mqtt-client-dotnet-core

Then plugged the values from my cluster instance but I am a getting connection timeout on startup.

Here is what my service configuration looks like:

public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services)
{
    services.AddMqttClientServiceWithConfig(aspOptionBuilder =>
    {
        //var clientSettinigs = AppSettingsProvider.ClientSettings;
        //var brokerHostSettings = AppSettingsProvider.BrokerHostSettings;

        aspOptionBuilder
        .WithCredentials("Test1", "xxxxx") //clientSettinigs.UserName, clientSettinigs.Password)
        .WithClientId("clientId-jqE8uIw6Pp") //clientSettinigs.Id)
        .WithTcpServer("xxxxxxxxxxxxxx.s2.eu.hivemq.cloud", 8884); //brokerHostSettings.Host, brokerHostSettings.Port);
    });
    return services;
}

private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<AspCoreMqttClientOptionBuilder> configure)
{
    services.AddSingleton<IMqttClientOptions>(serviceProvider =>
    {
        var optionBuilder = new AspCoreMqttClientOptionBuilder(serviceProvider);
        configure(optionBuilder);
        return optionBuilder.Build();
    });
    services.AddSingleton<MqttClientService>();
    services.AddSingleton<IHostedService>(serviceProvider =>
    {
        return serviceProvider.GetService<MqttClientService>();
    });
    services.AddSingleton<MqttClientServiceProvider>(serviceProvider =>
    {
        var mqttClientService = serviceProvider.GetService<MqttClientService>();
        var mqttClientServiceProvider = new MqttClientServiceProvider(mqttClientService);
        return mqttClientServiceProvider;
    });
    return services;
}

I am not sure where I am going wrong, any help would be greatly appreciated.

You appear to be trying to connect to the WebSocket endpoint (port 8884) in your code, when I suspect you really should be using the normal TLS endpoint (port 8883)

Also you will need to use different clientid values if you want to have both clients connected at the same time as having matching will mean the clients will continuously kick each other off the broker.

(edit: on looking closer the client ids are actually different, but only in the last char)

I had this issue in two days ago and it seems coming form TLS confgurations/settings . By the way, my Startup.cs service injections and some configurations were same with yours. I have.NetCore app and I am trying to connect my own hivemq broker (cloud side).

In this case we need to add additional option to our mqtt client option build phase.

When I add this code, Auth problems gone.

.WithTls();

Here is part of the client option codes should like that

AddMqttClientServiceWithConfig(services,optionBuilder =>
        {
            var clientSettings = BrokerAppSettingsProvider.BrokerClientSettings;
            var brokerHostSettings = BrokerAppSettingsProvider.BrokerHostSettings;

            optionBuilder
            .WithCredentials(clientSettings.UserName, clientSettings.Password)
            .WithTls()                
            .WithTcpServer(brokerHostSettings.Host, brokerHostSettings.Port);
        });
        return services;

We can consider this as a different solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM