Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

56 lignes
1.9 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.Diagnostics;
  9. using MQTTnet.Core.Server;
  10. namespace MQTTnet.TestApp.AspNetCore2
  11. {
  12. public class Startup
  13. {
  14. public void ConfigureServices(IServiceCollection services)
  15. {
  16. }
  17. public async void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  18. {
  19. loggerFactory.AddConsole(LogLevel.Debug);
  20. MqttNetTrace.TraceMessagePublished += (s, e) =>
  21. {
  22. Debug.WriteLine($">> [{e.TraceMessage.Timestamp}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}");
  23. if (e.TraceMessage.Exception != null)
  24. {
  25. Debug.WriteLine(e.TraceMessage.Exception.Message);
  26. }
  27. };
  28. var trace = new MqttNetTrace();
  29. var adapter = new MqttWebSocketServerAdapter(trace);
  30. var options = new MqttServerOptions();
  31. var mqttServer = new MqttServer(options, new List<IMqttServerAdapter> { adapter }, new MqttNetTrace());
  32. await mqttServer.StartAsync();
  33. app.UseWebSockets();
  34. app.Use(async (context, next) =>
  35. {
  36. if (context.WebSockets.IsWebSocketRequest)
  37. {
  38. using (var webSocket = await context.WebSockets.AcceptWebSocketAsync())
  39. {
  40. await adapter.AcceptWebSocketAsync(webSocket);
  41. }
  42. }
  43. else
  44. {
  45. await next();
  46. }
  47. });
  48. }
  49. }
  50. }