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.
 
 
 
 

96 regels
3.6 KiB

  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. }