Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

107 řádky
3.2 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. File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
  75. return Task.FromResult(0);
  76. }
  77. public Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
  78. {
  79. IList<MqttApplicationMessage> retainedMessages;
  80. if (File.Exists(Filename))
  81. {
  82. var json = File.ReadAllText(Filename);
  83. retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
  84. }
  85. else
  86. {
  87. retainedMessages = new List<MqttApplicationMessage>();
  88. }
  89. return Task.FromResult(retainedMessages);
  90. }
  91. }
  92. }