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.

Startup.cs 2.3 KiB

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