25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

MainPage.xaml.cs 5.5 KiB

7 yıl önce
7 yıl önce
7 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. MqttNetTrace.TraceMessagePublished += OnTraceMessagePublished;
  19. }
  20. private async void OnTraceMessagePublished(object sender, MqttNetTraceMessagePublishedEventArgs 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. MqttClientOptions options = null;
  35. if (UseTcp.IsChecked == true)
  36. {
  37. options = new MqttClientTcpOptions
  38. {
  39. Server = Server.Text
  40. };
  41. }
  42. if (UseWs.IsChecked == true)
  43. {
  44. options = new MqttClientWebSocketOptions
  45. {
  46. Uri = Server.Text
  47. };
  48. }
  49. if (options == null)
  50. {
  51. throw new InvalidOperationException();
  52. }
  53. options.UserName = User.Text;
  54. options.Password = Password.Text;
  55. options.ClientId = ClientId.Text;
  56. options.TlsOptions.UseTls = UseTls.IsChecked == true;
  57. options.TlsOptions.IgnoreCertificateChainErrors = true;
  58. options.TlsOptions.IgnoreCertificateRevocationErrors = true;
  59. options.TlsOptions.AllowUntrustedCertificates = true;
  60. try
  61. {
  62. if (_mqttClient != null)
  63. {
  64. await _mqttClient.DisconnectAsync();
  65. }
  66. var factory = new MqttClientFactory();
  67. _mqttClient = factory.CreateMqttClient(options);
  68. await _mqttClient.ConnectAsync(options);
  69. }
  70. catch (Exception exception)
  71. {
  72. Trace.Text += exception + Environment.NewLine;
  73. }
  74. }
  75. private async void Publish(object sender, RoutedEventArgs e)
  76. {
  77. if (_mqttClient == null)
  78. {
  79. return;
  80. }
  81. try
  82. {
  83. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  84. if (QoS1.IsChecked == true)
  85. {
  86. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  87. }
  88. if (QoS2.IsChecked == true)
  89. {
  90. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  91. }
  92. var payload = new byte[0];
  93. if (Text.IsChecked == true)
  94. {
  95. payload = Encoding.UTF8.GetBytes(Payload.Text);
  96. }
  97. if (Base64.IsChecked == true)
  98. {
  99. payload = Convert.FromBase64String(Payload.Text);
  100. }
  101. var message = new MqttApplicationMessage(
  102. Topic.Text,
  103. payload,
  104. qos,
  105. Retain.IsChecked == true);
  106. await _mqttClient.PublishAsync(message);
  107. }
  108. catch (Exception exception)
  109. {
  110. Trace.Text += exception + Environment.NewLine;
  111. }
  112. }
  113. private async void Disconnect(object sender, RoutedEventArgs e)
  114. {
  115. try
  116. {
  117. await _mqttClient.DisconnectAsync();
  118. }
  119. catch (Exception exception)
  120. {
  121. Trace.Text += exception + Environment.NewLine;
  122. }
  123. }
  124. private void Clear(object sender, RoutedEventArgs e)
  125. {
  126. Trace.Text = string.Empty;
  127. }
  128. private async void Subscribe(object sender, RoutedEventArgs e)
  129. {
  130. if (_mqttClient == null)
  131. {
  132. return;
  133. }
  134. try
  135. {
  136. var qos = MqttQualityOfServiceLevel.AtMostOnce;
  137. if (SubscribeQoS1.IsChecked == true)
  138. {
  139. qos = MqttQualityOfServiceLevel.AtLeastOnce;
  140. }
  141. if (SubscribeQoS2.IsChecked == true)
  142. {
  143. qos = MqttQualityOfServiceLevel.ExactlyOnce;
  144. }
  145. await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos));
  146. }
  147. catch (Exception exception)
  148. {
  149. Trace.Text += exception + Environment.NewLine;
  150. }
  151. }
  152. private async void Unsubscribe(object sender, RoutedEventArgs e)
  153. {
  154. if (_mqttClient == null)
  155. {
  156. return;
  157. }
  158. try
  159. {
  160. await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text);
  161. }
  162. catch (Exception exception)
  163. {
  164. Trace.Text += exception + Environment.NewLine;
  165. }
  166. }
  167. }
  168. }