|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- using System;
- using Windows.UI.Core;
- using Windows.UI.Xaml;
- using MQTTnet.Core.Client;
- using MQTTnet.Core.Diagnostics;
-
- namespace MQTTnet.TestApp.UniversalWindows
- {
- public sealed partial class MainPage
- {
- private IMqttClient _mqttClient;
-
- public MainPage()
- {
- InitializeComponent();
-
- MqttTrace.TraceMessagePublished += OnTraceMessagePublished;
- }
-
- private async void OnTraceMessagePublished(object sender, MqttTraceMessagePublishedEventArgs e)
- {
- await Trace.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
- {
- var text = $"[{DateTime.Now:O}] [{e.Level}] [{e.Source}] [{e.ThreadId}] [{e.Message}]{Environment.NewLine}";
- if (e.Exception != null)
- {
- text += $"{e.Exception}{Environment.NewLine}";
- }
-
- Trace.Text += text;
- });
- }
-
- private async void Connect(object sender, RoutedEventArgs e)
- {
- var options = new MqttClientOptions
- {
- Server = Server.Text,
- UserName = User.Text,
- Password = Password.Text,
- ClientId = ClientId.Text
- };
-
- options.TlsOptions.UseTls = UseTls.IsChecked == true;
-
- try
- {
- if (_mqttClient != null)
- {
- await _mqttClient.DisconnectAsync();
- }
-
- var factory = new MqttClientFactory();
- _mqttClient = factory.CreateMqttClient(options);
- await _mqttClient.ConnectAsync();
- }
- catch (Exception exception)
- {
- Trace.Text += exception + Environment.NewLine;
- }
- }
- }
- }
|