Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

110 строки
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MQTTnet.Client;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.Server;
  9. using Newtonsoft.Json;
  10. namespace MQTTnet.TestApp.NetCore
  11. {
  12. public static class Program
  13. {
  14. public static void Main()
  15. {
  16. Console.WriteLine($"MQTTnet - TestApp.{TargetFrameworkInfoProvider.TargetFramework}");
  17. Console.WriteLine("1 = Start client");
  18. Console.WriteLine("2 = Start server");
  19. Console.WriteLine("3 = Start performance test");
  20. Console.WriteLine("4 = Start managed client");
  21. var pressedKey = Console.ReadKey(true);
  22. if (pressedKey.KeyChar == '1')
  23. {
  24. Task.Run(ClientTest.RunAsync);
  25. }
  26. else if (pressedKey.KeyChar == '2')
  27. {
  28. Task.Run(ServerTest.RunAsync);
  29. }
  30. else if (pressedKey.KeyChar == '3')
  31. {
  32. Task.Run(PerformanceTest.RunAsync);
  33. }
  34. else if (pressedKey.KeyChar == '4')
  35. {
  36. Task.Run(ManagedClientTest.RunAsync);
  37. }
  38. Thread.Sleep(Timeout.Infinite);
  39. }
  40. // This code is used at the Wiki on GitHub!
  41. // ReSharper disable once UnusedMember.Local
  42. private static async void WikiCode()
  43. {
  44. {
  45. var client = new MqttFactory().CreateMqttClient();
  46. var options = new MqttClientOptionsBuilder()
  47. .WithClientId("Client1")
  48. .WithTcpServer("broker.hivemq.com")
  49. .WithCredentials("bud", "%spencer%")
  50. .WithTls()
  51. .Build();
  52. await client.ConnectAsync(options);
  53. var message = new MqttApplicationMessageBuilder()
  54. .WithTopic("MyTopic")
  55. .WithPayload("Hello World")
  56. .WithExactlyOnceQoS()
  57. .WithRetainFlag()
  58. .Build();
  59. await client.PublishAsync(message);
  60. }
  61. {
  62. var factory = new MqttFactory();
  63. var client = factory.CreateMqttClient();
  64. }
  65. }
  66. }
  67. public class RetainedMessageHandler : IMqttServerStorage
  68. {
  69. private const string Filename = "C:\\MQTT\\RetainedMessages.json";
  70. public Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
  71. {
  72. var directory = Path.GetDirectoryName(Filename);
  73. if (!Directory.Exists(directory))
  74. {
  75. Directory.CreateDirectory(directory);
  76. }
  77. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  78. return Task.FromResult(0);
  79. }
  80. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  81. {
  82. IList<MqttApplicationMessage> retainedMessages;
  83. if (File.Exists(Filename))
  84. {
  85. var json = File.ReadAllText(Filename);
  86. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  87. }
  88. else
  89. {
  90. retainedMessages = new List<MqttApplicationMessage>();
  91. }
  92. return Task.FromResult(retainedMessages);
  93. }
  94. }
  95. }