From bfb6ec885692111bcb3d8ff78547ff6ee3089410 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 4 Jul 2018 22:02:42 +0200 Subject: [PATCH] Update UWP test app. --- .../MQTTnet.TestApp.UniversalWindows.csproj | 2 +- .../MainPage.xaml | 127 ++++++++++++------ .../MainPage.xaml.cs | 100 ++++++++++---- 3 files changed, 163 insertions(+), 66 deletions(-) diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj index d709083..a6f4566 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MQTTnet.TestApp.UniversalWindows.csproj @@ -142,7 +142,7 @@ - 6.1.4 + 6.1.5 3.0.0 diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml index 41617df..c99ebd8 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml @@ -5,7 +5,6 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:server="using:MQTTnet.Server" - xmlns:interop="using:Windows.UI.Xaml.Interop" d:DesignHeight="800" d:DesignWidth="800" mc:Ignorable="d"> @@ -19,26 +18,55 @@ - Server: - - Port: - - User: - - Password: - - ClientId: - - Clean session: - - Keep alive interval: - - - - TCP - WS - Use TLS - + + + + + + + + + + + + + + + + + + + Server: + + + Port: + + + Username: + + + Password: + + + Client ID: + + + Keep Alive interval: + + + Protocol: + + TCP + WS + Use TLS + + + + Clean session + Use managed client @@ -49,25 +77,46 @@ - Topic: - + + + + + + + + + + + + + + + + Topic: + + + Payload: + + + Payload format: + + Plain text + Base64 string + + + QoS level: + + 0 (At most once) + 1 (At least once) + 2 (Exactly once) + + + + Retain - Payload: - - - Text - Base64 - - - Retain: - - - QoS: - - 0 (At most once) - 1 (At least once) - 2 (Exactly once) - diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 387364a..b557fe0 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.ObjectModel; +using System.IO; using System.Text; using System.Threading.Tasks; using Windows.Security.Cryptography.Certificates; @@ -25,12 +26,15 @@ namespace MQTTnet.TestApp.UniversalWindows private readonly ObservableCollection _sessions = new ObservableCollection(); private IMqttClient _mqttClient; + private IManagedMqttClient _managedMqttClient; private IMqttServer _mqttServer; public MainPage() { InitializeComponent(); + ClientId.Text = Guid.NewGuid().ToString("D"); + MqttNetGlobalLogger.LogMessagePublished += OnTraceMessagePublished; } @@ -81,7 +85,10 @@ namespace MQTTnet.TestApp.UniversalWindows AllowUntrustedCertificates = true }; - var options = new MqttClientOptions { ClientId = ClientId.Text }; + var options = new MqttClientOptions + { + ClientId = ClientId.Text + }; if (UseTcp.IsChecked == true) { @@ -127,12 +134,28 @@ namespace MQTTnet.TestApp.UniversalWindows } var factory = new MqttFactory(); - _mqttClient = factory.CreateMqttClient(); - _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived; - _mqttClient.Connected += OnConnected; - _mqttClient.Disconnected += OnDisconnected; - await _mqttClient.ConnectAsync(options); + if (UseManagedClient.IsChecked == true) + { + _managedMqttClient = factory.CreateManagedMqttClient(); + _managedMqttClient.ApplicationMessageReceived += OnApplicationMessageReceived; + _managedMqttClient.Connected += OnConnected; + _managedMqttClient.Disconnected += OnDisconnected; + + await _managedMqttClient.StartAsync(new ManagedMqttClientOptions + { + ClientOptions = options + }); + } + else + { + _mqttClient = factory.CreateMqttClient(); + _mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived; + _mqttClient.Connected += OnConnected; + _mqttClient.Disconnected += OnDisconnected; + + await _mqttClient.ConnectAsync(options); + } } catch (Exception exception) { @@ -171,11 +194,6 @@ namespace MQTTnet.TestApp.UniversalWindows private async void Publish(object sender, RoutedEventArgs e) { - if (_mqttClient == null) - { - return; - } - try { var qos = MqttQualityOfServiceLevel.AtMostOnce; @@ -190,7 +208,7 @@ namespace MQTTnet.TestApp.UniversalWindows } var payload = new byte[0]; - if (Text.IsChecked == true) + if (PlainText.IsChecked == true) { payload = Encoding.UTF8.GetBytes(Payload.Text); } @@ -207,7 +225,15 @@ namespace MQTTnet.TestApp.UniversalWindows .WithRetainFlag(Retain.IsChecked == true) .Build(); - await _mqttClient.PublishAsync(message); + if (_mqttClient != null) + { + await _mqttClient.PublishAsync(message); + } + + if (_managedMqttClient != null) + { + await _managedMqttClient.PublishAsync(message); + } } catch (Exception exception) { @@ -219,7 +245,19 @@ namespace MQTTnet.TestApp.UniversalWindows { try { - await _mqttClient.DisconnectAsync(); + if (_mqttClient != null) + { + await _mqttClient.DisconnectAsync(); + _mqttClient.Dispose(); + _mqttClient = null; + } + + if (_managedMqttClient != null) + { + await _managedMqttClient.StopAsync(); + _managedMqttClient.Dispose(); + _managedMqttClient = null; + } } catch (Exception exception) { @@ -239,11 +277,6 @@ namespace MQTTnet.TestApp.UniversalWindows private async void Subscribe(object sender, RoutedEventArgs e) { - if (_mqttClient == null) - { - return; - } - try { var qos = MqttQualityOfServiceLevel.AtMostOnce; @@ -257,7 +290,15 @@ namespace MQTTnet.TestApp.UniversalWindows qos = MqttQualityOfServiceLevel.ExactlyOnce; } - await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos)); + if (_mqttClient != null) + { + await _mqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos)); + } + + if (_managedMqttClient != null) + { + await _managedMqttClient.SubscribeAsync(new TopicFilter(SubscribeTopic.Text, qos)); + } } catch (Exception exception) { @@ -267,14 +308,17 @@ namespace MQTTnet.TestApp.UniversalWindows private async void Unsubscribe(object sender, RoutedEventArgs e) { - if (_mqttClient == null) - { - return; - } - try { - await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text); + if (_mqttClient != null) + { + await _mqttClient.UnsubscribeAsync(SubscribeTopic.Text); + } + + if (_managedMqttClient != null) + { + await _managedMqttClient.UnsubscribeAsync(SubscribeTopic.Text); + } } catch (Exception exception) { @@ -398,6 +442,8 @@ namespace MQTTnet.TestApp.UniversalWindows ListViewSessions.DataContext = _sessions; } + #region Wiki Code + private async Task WikiCode() { { @@ -632,5 +678,7 @@ namespace MQTTnet.TestApp.UniversalWindows await mqttClient.StartAsync(options); } } + + #endregion } }