Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

64 lignes
1.8 KiB

  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 MqttClient _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. }