No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

175 líneas
5.0 KiB

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