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.
 
 
 
 

56 lines
1.6 KiB

  1. using System;
  2. using MQTTnet.Core.Diagnostics;
  3. using MQTTnet.Core.Protocol;
  4. using MQTTnet.Core.Server;
  5. namespace MQTTnet.TestMqttServer
  6. {
  7. public static class Program
  8. {
  9. public static void Main(string[] arguments)
  10. {
  11. MqttTrace.TraceMessagePublished += (s, e) =>
  12. {
  13. Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
  14. if (e.Exception != null)
  15. {
  16. Console.WriteLine(e.Exception);
  17. }
  18. };
  19. try
  20. {
  21. var options = new MqttServerOptions
  22. {
  23. ConnectionValidator = p =>
  24. {
  25. if (p.ClientId == "SpecialClient")
  26. {
  27. if (p.Username != "USER" || p.Password != "PASS")
  28. {
  29. return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
  30. }
  31. }
  32. return MqttConnectReturnCode.ConnectionAccepted;
  33. }
  34. };
  35. var mqttServer = new MqttServerFactory().CreateMqttServer(options);
  36. mqttServer.Start();
  37. Console.WriteLine("Press any key to exit.");
  38. Console.ReadLine();
  39. mqttServer.Stop();
  40. }
  41. catch (Exception e)
  42. {
  43. Console.WriteLine(e);
  44. }
  45. Console.ReadLine();
  46. }
  47. }
  48. }