You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainPage.xaml.cs 1.8 KiB

пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
пре 7 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using Windows.UI.Core;
  3. using Windows.UI.Xaml;
  4. using MQTTnet.Core.Client;
  5. using MQTTnet.Core.Diagnostics;
  6. namespace MQTTnet.TestApp.UniversalWindows
  7. {
  8. public sealed partial class MainPage
  9. {
  10. private IMqttClient _mqttClient;
  11. public MainPage()
  12. {
  13. InitializeComponent();
  14. MqttTrace.TraceMessagePublished += OnTraceMessagePublished;
  15. }
  16. private async void OnTraceMessagePublished(object sender, MqttTraceMessagePublishedEventArgs e)
  17. {
  18. await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
  19. {
  20. var text = $"[{DateTime.Now:O}] [{e.Level}] [{e.Source}] [{e.ThreadId}] [{e.Message}]{Environment.NewLine}";
  21. if (e.Exception != null)
  22. {
  23. text += $"{e.Exception}{Environment.NewLine}";
  24. }
  25. Trace.Text += text;
  26. });
  27. }
  28. private async void Connect(object sender, RoutedEventArgs e)
  29. {
  30. var options = new MqttClientOptions
  31. {
  32. Server = Server.Text,
  33. UserName = User.Text,
  34. Password = Password.Text,
  35. ClientId = ClientId.Text
  36. };
  37. options.TlsOptions.UseTls = UseTls.IsChecked == true;
  38. try
  39. {
  40. if (_mqttClient != null)
  41. {
  42. await _mqttClient.DisconnectAsync();
  43. }
  44. var factory = new MqttClientFactory();
  45. _mqttClient = factory.CreateMqttClient(options);
  46. await _mqttClient.ConnectAsync();
  47. }
  48. catch (Exception exception)
  49. {
  50. Trace.Text += exception + Environment.NewLine;
  51. }
  52. }
  53. }
  54. }