Startup.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Runtime.Versioning;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.FileProviders;
  10. using Microsoft.Extensions.Logging;
  11. using MQTTnet.AspNetCore;
  12. using MQTTnet.Server;
  13. namespace MQTTnet.TestApp.AspNetCore2
  14. {
  15. public class Startup
  16. {
  17. // In class _Startup_ of the ASP.NET Core 2.0 project.
  18. public void ConfigureServices(IServiceCollection services)
  19. {
  20. services
  21. .AddHostedMqttServer(mqttServer => mqttServer.WithDefaultEndpointPort(1883)).
  22. AddMqttTcpServerAdapter().
  23. AddMqttWebSocketServerAdapter()
  24. .AddMqttConnectionHandler()
  25. .AddConnections();
  26. //// This adds a hosted mqtt server to the services
  27. //services.AddHostedMqttServer(builder => builder.WithDefaultEndpointPort(1883));
  28. //// This adds TCP server support based on System.Net.Socket
  29. //services.AddMqttTcpServerAdapter();
  30. //// This adds websocket support
  31. //services.AddMqttWebSocketServerAdapter();
  32. }
  33. // In class _Startup_ of the ASP.NET Core 3.1 project.
  34. #if NETCOREAPP3_1
  35. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  36. {
  37. app.UseRouting();
  38. app.UseEndpoints(endpoints =>
  39. {
  40. endpoints.MapMqtt("/mqtt");
  41. });
  42. #else
  43. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  44. {
  45. app.UseConnections(c => c.MapMqtt("/mqtt"));
  46. #endif
  47. app.UseMqttServer(server =>
  48. {
  49. server.StartedHandler = new MqttServerStartedHandlerDelegate(async args =>
  50. {
  51. var frameworkName = GetType().Assembly.GetCustomAttribute<TargetFrameworkAttribute>()?
  52. .FrameworkName;
  53. var msg = new MqttApplicationMessageBuilder()
  54. .WithPayload($"Mqtt hosted on {frameworkName} is awesome")
  55. .WithTopic("message");
  56. while (true)
  57. {
  58. try
  59. {
  60. await server.PublishAsync(msg.Build());
  61. msg.WithPayload($"Mqtt hosted on {frameworkName} is still awesome at {DateTime.Now}");
  62. }
  63. catch (Exception e)
  64. {
  65. Console.WriteLine(e);
  66. }
  67. finally
  68. {
  69. await Task.Delay(TimeSpan.FromSeconds(2));
  70. }
  71. }
  72. });
  73. });
  74. app.Use((context, next) =>
  75. {
  76. if (context.Request.Path == "/")
  77. {
  78. context.Request.Path = "/Index.html";
  79. }
  80. return next();
  81. });
  82. app.UseStaticFiles();
  83. app.UseStaticFiles(new StaticFileOptions
  84. {
  85. RequestPath = "/node_modules",
  86. FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules"))
  87. });
  88. }
  89. }
  90. }