You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

60 lines
1.7 KiB

  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.FileProviders;
  8. using Microsoft.Extensions.Logging;
  9. using MQTTnet.AspnetCore;
  10. using MQTTnet.Core;
  11. using MQTTnet.Core.Client;
  12. namespace MQTTnet.TestApp.AspNetCore2
  13. {
  14. public class Startup
  15. {
  16. public void ConfigureServices(IServiceCollection services)
  17. {
  18. services.AddHostedMqttServer();
  19. }
  20. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  21. {
  22. app.UseMqttEndpoint();
  23. app.UseMqttServer(async server =>
  24. {
  25. var msg = new MqttApplicationMessageBuilder()
  26. .WithPayload("Mqtt is awesome")
  27. .WithTopic("message");
  28. while (true)
  29. {
  30. server.PublishAsync(msg.Build()).Wait();
  31. await Task.Delay(TimeSpan.FromSeconds(2));
  32. msg.WithPayload("Mqtt is still awesome at " + DateTime.Now);
  33. }
  34. });
  35. app.Use((context, next) =>
  36. {
  37. if (context.Request.Path == "/")
  38. {
  39. context.Request.Path = "/Index.html";
  40. }
  41. return next();
  42. });
  43. app.UseStaticFiles();
  44. app.UseStaticFiles( new StaticFileOptions()
  45. {
  46. RequestPath = "/node_modules",
  47. FileProvider = new PhysicalFileProvider( Path.Combine(env.ContentRootPath, "node_modules" ) )
  48. } );
  49. }
  50. }
  51. }