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

176 строки
5.0 KiB

  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Windows.UI.Core;
  5. using Windows.UI.Xaml;
  6. using MQTTnet.Core;
  7. using MQTTnet.Core.Client;
  8. using MQTTnet.Core.Diagnostics;
  9. using MQTTnet.Core.Packets;
  10. using MQTTnet.Core.Protocol;
  11. namespace MQTTnet.TestApp.UniversalWindows
  12. {
  13. public sealed partial class MainPage
  14. {
  15. private IMqttClient _mqttClient;
  16. public MainPage()
  17. {
  18. InitializeComponent();
  19. MqttTrace.TraceMessagePublished += OnTraceMessagePublished;
  20. }
  21. private async void OnTraceMessagePublished(object sender, MqttTraceMessagePublishedEventArgs e)
  22. {
  23. await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
  24. {
  25. var text = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] [{e.Level}] [{e.Source}] [{e.ThreadId}] [{e.Message}]{Environment.NewLine}";
  26. if (e.Exception != null)
  27. {
  28. text += $"{e.Exception}{Environment.NewLine}";
  29. }
  30. Trace.Text += text;
  31. });
  32. }
  33. private async void Connect(object sender, RoutedEventArgs e)
  34. {
  35. var options = new MqttClientOptions
  36. {
  37. Server = Server.Text,
  38. UserName = User.Text,
  39. Password = Password.Text,
  40. ClientId = ClientId.Text,
  41. TlsOptions = { UseTls = UseTls.IsChecked == true },
  42. ConnectionType = UseTcp.IsChecked == true ? MqttConnectionType.Tcp : MqttConnectionType.Ws
  43. };
  44. try
  45. {
  46. if (_mqttClient != null)
  47. {
  48. await _mqttClient.DisconnectAsync();
  49. }
  50. var factory = new MqttClientFactory();
  51. _mqttClient = factory.CreateMqttClient(options);
  52. await _mqttClient.ConnectAsync();
  53. }
  54. catch (Exception exception)
  55. {
  56. Trace.Text += exception + Environment.NewLine;
  57. }
  58. }
  59. private async void Publish(object sender, RoutedEventArgs e)
  60. {
  61. if (_mqttClient == null)
  62. {
  63. return;
  64. }
  65. try
  66. {
  67. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  68. if (QoS1.IsChecked == true)
  69. {
  70. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  71. }
  72. if (QoS2.IsChecked == true)
  73. {
  74. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  75. }
  76. var payload = new byte[0];
  77. if (Text.IsChecked == true)
  78. {
  79. payload = Encoding.UTF8.GetBytes(Payload.Text);
  80. }
  81. if (Base64.IsChecked == true)
  82. {
  83. payload = Convert.FromBase64String(Payload.Text);
  84. }
  85. var message = new MqttApplicationMessage(
  86. Topic.Text,
  87. payload,
  88. qos,
  89. Retain.IsChecked == true);
  90. await _mqttClient.PublishAsync(message);
  91. }
  92. catch (Exception exception)
  93. {
  94. Trace.Text += exception + Environment.NewLine;
  95. }
  96. }
  97. private async void Disconnect(object sender, RoutedEventArgs e)
  98. {
  99. try
  100. {
  101. await _mqttClient.DisconnectAsync();
  102. }
  103. catch (Exception exception)
  104. {
  105. Trace.Text += exception + Environment.NewLine;
  106. }
  107. }
  108. private void Clear(object sender, RoutedEventArgs e)
  109. {
  110. Trace.Text = string.Empty;
  111. }
  112. private async void Subscribe(object sender, RoutedEventArgs e)
  113. {
  114. if (_mqttClient == null)
  115. {
  116. return;
  117. }
  118. try
  119. {
  120. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  121. if (SubscribeQoS1.IsChecked == true)
  122. {
  123. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  124. }
  125. if (SubscribeQoS2.IsChecked == true)
  126. {
  127. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  128. }
  129. await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos));
  130. }
  131. catch (Exception exception)
  132. {
  133. Trace.Text += exception + Environment.NewLine;
  134. }
  135. }
  136. private async void Unsubscribe(object sender, RoutedEventArgs e)
  137. {
  138. if (_mqttClient == null)
  139. {
  140. return;
  141. }
  142. try
  143. {
  144. await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text);
  145. }
  146. catch (Exception exception)
  147. {
  148. Trace.Text += exception + Environment.NewLine;
  149. }
  150. }
  151. }
  152. }