Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ServerTest.cs 3.6 KiB

7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
7 anos atrás
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using MQTTnet.Core.Protocol;
  5. using MQTTnet.Core.Server;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. namespace MQTTnet.TestApp.NetCore
  9. {
  10. public static class ServerTest
  11. {
  12. public static Task RunAsync()
  13. {
  14. try
  15. {
  16. var services = new ServiceCollection()
  17. .AddMqttServer()
  18. .AddLogging();
  19. services.Configure<MqttServerOptions>(options =>
  20. {
  21. options.ConnectionValidator = p =>
  22. {
  23. if (p.ClientId == "SpecialClient")
  24. {
  25. if (p.Username != "USER" || p.Password != "PASS")
  26. {
  27. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  28. }
  29. }
  30. return MqttConnectReturnCode.ConnectionAccepted;
  31. };
  32. options.Storage = new RetainedMessageHandler();
  33. // Extend the timestamp for all messages from clients.
  34. options.ApplicationMessageInterceptor = context =>
  35. {
  36. if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#"))
  37. {
  38. // Replace the payload with the timestamp. But also extending a JSON
  39. // based payload with the timestamp is a suitable use case.
  40. context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
  41. }
  42. };
  43. // Protect several topics from being subscribed from every client.
  44. options.SubscriptionsInterceptor = context =>
  45. {
  46. if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin")
  47. {
  48. context.AcceptSubscription = false;
  49. }
  50. if (context.TopicFilter.Topic.StartsWith("the/secret/stuff") && context.ClientId != "Imperator")
  51. {
  52. context.AcceptSubscription = false;
  53. context.CloseConnection = true;
  54. }
  55. };
  56. });
  57. var serviceProvider = services.BuildServiceProvider();
  58. serviceProvider.GetRequiredService<ILoggerFactory>().AddConsole();
  59. //var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
  60. //options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
  61. //options.ConnectionBacklog = 5;
  62. //options.DefaultEndpointOptions.IsEnabled = true;
  63. //options.TlsEndpointOptions.IsEnabled = false;
  64. var mqttServer = new MqttFactory(serviceProvider).CreateMqttServer();
  65. mqttServer.ClientDisconnected += (s, e) =>
  66. {
  67. Console.Write("Client disconnected event fired.");
  68. };
  69. mqttServer.StartAsync();
  70. Console.WriteLine("Press any key to exit.");
  71. Console.ReadLine();
  72. mqttServer.StopAsync();
  73. }
  74. catch (Exception e)
  75. {
  76. Console.WriteLine(e);
  77. }
  78. Console.ReadLine();
  79. return Task.FromResult(0);
  80. }
  81. }
  82. }