Howto use MQTTNET with WSS

45 Views Asked by At

i build an app in net7 to use the mqttserver from MQTTNET. The connection over WS and TCP (port 1883) workes, but the Secure WSS not. I have generate a selfSigned certificate with opensssl. When this workes i want to generate and import the certificate with letsencrypt if this is possible?

Program.cs


public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseKestrel(option =>
                {
                    option.ListenAnyIP(1883, l => l.UseMqtt());
                    option.ListenAnyIP(5001, listenOptions =>
                    {
                        var path = 
                        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        var certPath = Path.Combine(path, "certificate.pfx");
                        var cert = new X509Certificate2(certPath, "12345");

                        listenOptions.UseHttps(cert);
                    });
                    option.ListenAnyIP(5005); // default HTTP Port
                });
                webBuilder.UseStartup<Startup>();
            });
}

Startup.cs

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, 
        MqttServerController mqttController)
     {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }

     app.UseRouting();

     app.UseEndpoints(
         endpoints =>
         {
             endpoints.MapConnectionHandler<MqttConnectionHandler>(
             "/mqtt",
                 httpConnectionDispatcherOptions => 
                     httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector =
                     protocolList => protocolList.FirstOrDefault() ?? string.Empty);
         });

     app.UseMqttServer(
         server =>
         {
             server.ValidatingConnectionAsync += mqttController.ValidateConnection;
             server.ClientConnectedAsync += mqttController.OnClientConnected;
             server.InterceptingPublishAsync += 
                  mqttController.InterceptApplicationMessagePublishAsync;
         });
      }

       public void ConfigureServices(IServiceCollection services)
       {

           services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

           services.AddSingleton<DataContext>();
           services.AddSingleton<IUserRepository, UserRepository>();
           services.AddSingleton<IUserService, UserService>();
           services.AddSingleton<IDeviceRepository, DeviceRepository>();
           services.AddSingleton<IDeviceService, DeviceService>();

           services.AddHostedMqttServer(
                optionsBuilder =>
                {
                    optionsBuilder.WithoutDefaultEndpoint();
                });

           services.AddMqttConnectionHandler();
           services.AddMqttWebSocketServerAdapter();
           services.AddConnections();

           services.AddSingleton<MqttServerController>();
       }

I have written an Angular APP with the ngx-mqtt (javascsript lib) Here is bowsers output enter image description here

What i missing or doing wrong in my implementation? Thanks Markus

0

There are 0 best solutions below