Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

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