25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

113 lines
3.4 KiB

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