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.
 
 
 
 

74 line
2.2 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 MQTTnet.AspNetCore;
  9. using MQTTnet.Core;
  10. namespace MQTTnet.TestApp.AspNetCore2
  11. {
  12. public class Startup
  13. {
  14. // In class _Startup_ of the ASP.NET Core 2.0 project.
  15. public void ConfigureServices(IServiceCollection services)
  16. {
  17. services.AddHostedMqttServer();
  18. }
  19. // In class _Startup_ of the ASP.NET Core 2.0 project.
  20. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  21. {
  22. app.UseMqttEndpoint();
  23. app.UseMqttServer(server =>
  24. {
  25. server.Started += async (sender, args) =>
  26. {
  27. var msg = new MqttApplicationMessageBuilder()
  28. .WithPayload("Mqtt is awesome")
  29. .WithTopic("message");
  30. while (true)
  31. {
  32. try
  33. {
  34. await server.PublishAsync(msg.Build());
  35. msg.WithPayload("Mqtt is still awesome at " + DateTime.Now);
  36. }
  37. catch (Exception e)
  38. {
  39. Console.WriteLine(e);
  40. }
  41. finally
  42. {
  43. await Task.Delay(TimeSpan.FromSeconds(2));
  44. }
  45. }
  46. };
  47. });
  48. app.Use((context, next) =>
  49. {
  50. if (context.Request.Path == "/")
  51. {
  52. context.Request.Path = "/Index.html";
  53. }
  54. return next();
  55. });
  56. app.UseStaticFiles();
  57. app.UseStaticFiles(new StaticFileOptions
  58. {
  59. RequestPath = "/node_modules",
  60. FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "node_modules"))
  61. });
  62. }
  63. }
  64. }