Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

48 righe
1.6 KiB

  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using Microsoft.AspNetCore.Builder;
  4. using Microsoft.AspNetCore.Hosting;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using MQTTnet.Core.Adapter;
  8. using MQTTnet.Core.Server;
  9. namespace MQTTnet.TestApp.AspNetCore2
  10. {
  11. public class Startup
  12. {
  13. public void ConfigureServices(IServiceCollection services)
  14. {
  15. services.AddMqttServer();
  16. services.AddSingleton<MqttWebSocketServerAdapter>();
  17. services.AddSingleton<IMqttServerAdapter, MqttWebSocketServerAdapter>();
  18. }
  19. public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  20. {
  21. loggerFactory.AddConsole(LogLevel.Debug);
  22. loggerFactory.AddDebug();
  23. var adapter = app.ApplicationServices.GetService<MqttWebSocketServerAdapter>();
  24. var mqttServer = app.ApplicationServices.GetService<IMqttServer>();
  25. await mqttServer.StartAsync();
  26. app.UseWebSockets();
  27. app.Use(async (context, next) =>
  28. {
  29. if (context.WebSockets.IsWebSocketRequest)
  30. {
  31. using (var webSocket = await context.WebSockets.AcceptWebSocketAsync())
  32. {
  33. await adapter.AcceptWebSocketAsync(webSocket);
  34. }
  35. }
  36. else
  37. {
  38. await next();
  39. }
  40. });
  41. }
  42. }
  43. }