mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Update UWP test app.
This commit is contained in:
@@ -142,7 +142,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||
<Version>6.1.4</Version>
|
||||
<Version>6.1.5</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls">
|
||||
<Version>3.0.0</Version>
|
||||
|
||||
@@ -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 @@
|
||||
<Pivot Grid.Row="0">
|
||||
<PivotItem Header="Connection">
|
||||
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<TextBlock>Server:</TextBlock>
|
||||
<TextBox x:Name="Server"></TextBox>
|
||||
<TextBlock>Port:</TextBlock>
|
||||
<TextBox x:Name="Port" Text="1883"></TextBox>
|
||||
<TextBlock>User:</TextBlock>
|
||||
<TextBox x:Name="User"></TextBox>
|
||||
<TextBlock>Password:</TextBlock>
|
||||
<TextBox x:Name="Password"></TextBox>
|
||||
<TextBlock>ClientId:</TextBlock>
|
||||
<TextBox x:Name="ClientId"></TextBox>
|
||||
<TextBlock>Clean session:</TextBlock>
|
||||
<CheckBox x:Name="CleanSession" IsChecked="True"></CheckBox>
|
||||
<TextBlock>Keep alive interval:</TextBlock>
|
||||
<TextBox x:Name="KeepAliveInterval" Text="5"></TextBox>
|
||||
<Grid>
|
||||
<Grid.Resources>
|
||||
<Style TargetType="TextBlock">
|
||||
<Setter Property="VerticalAlignment" Value="Center"></Setter>
|
||||
<Setter Property="Margin" Value="0, 0, 10, 0"></Setter>
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"></ColumnDefinition>
|
||||
<ColumnDefinition Width="*"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton x:Name="UseTcp" IsChecked="True" GroupName="connection">TCP</RadioButton>
|
||||
<RadioButton x:Name="UseWs" GroupName="connection">WS</RadioButton>
|
||||
<CheckBox x:Name="UseTls">Use TLS</CheckBox>
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Column="0" Grid.Row="0">Server:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="0" x:Name="Server" Text="localhost"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="1">Port:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="1" x:Name="Port" Text="1883"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="2">Username:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="2" x:Name="User"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="3">Password:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="3" x:Name="Password"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="4">Client ID:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="4" x:Name="ClientId"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="5">Keep Alive interval:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="5" x:Name="KeepAliveInterval" Text="5"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="6">Protocol:</TextBlock>
|
||||
<StackPanel Grid.Column="1" Grid.Row="6" Orientation="Horizontal">
|
||||
<RadioButton x:Name="UseTcp" IsChecked="True" GroupName="connection">TCP</RadioButton>
|
||||
<RadioButton x:Name="UseWs" GroupName="connection">WS</RadioButton>
|
||||
<CheckBox x:Name="UseTls">Use TLS</CheckBox>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
<CheckBox x:Name="CleanSession" IsChecked="True">Clean session</CheckBox>
|
||||
<CheckBox x:Name="UseManagedClient" IsChecked="False">Use managed client</CheckBox>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Click="Connect" Width="120" Margin="0,0,10,0">Connect</Button>
|
||||
@@ -49,25 +77,46 @@
|
||||
|
||||
<PivotItem Header="Publish">
|
||||
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<TextBlock>Topic:</TextBlock>
|
||||
<TextBox x:Name="Topic"></TextBox>
|
||||
<Grid>
|
||||
<Grid.Resources>
|
||||
<Style TargetType="TextBlock">
|
||||
<Setter Property="VerticalAlignment" Value="Center"></Setter>
|
||||
<Setter Property="Margin" Value="0, 0, 10, 0"></Setter>
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"></ColumnDefinition>
|
||||
<ColumnDefinition Width="*"></ColumnDefinition>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock>Payload:</TextBlock>
|
||||
<TextBox x:Name="Payload"></TextBox>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton x:Name="Text" IsChecked="True" GroupName="payload">Text</RadioButton>
|
||||
<RadioButton x:Name="Base64" GroupName="payload">Base64</RadioButton>
|
||||
</StackPanel>
|
||||
<TextBlock Grid.Column="0" Grid.Row="0">Topic:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="0" x:Name="Topic"></TextBox>
|
||||
|
||||
<TextBlock>Retain:</TextBlock>
|
||||
<CheckBox x:Name="Retain"></CheckBox>
|
||||
<TextBlock Grid.Column="0" Grid.Row="1">Payload:</TextBlock>
|
||||
<TextBox Grid.Column="1" Grid.Row="1" x:Name="Payload"></TextBox>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="2">Payload format:</TextBlock>
|
||||
<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" x:Name="PlainText" IsChecked="True" GroupName="payload">Plain text</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="Base64" GroupName="payload">Base64 string</RadioButton>
|
||||
</StackPanel>
|
||||
|
||||
<TextBlock Grid.Column="0" Grid.Row="3">QoS level:</TextBlock>
|
||||
<StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="qos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS1" GroupName="qos">1 (At least once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS2" GroupName="qos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
<CheckBox x:Name="Retain">Retain</CheckBox>
|
||||
|
||||
<TextBlock>QoS:</TextBlock>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" IsChecked="True" GroupName="qos">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS1" GroupName="qos">1 (At least once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="QoS2" GroupName="qos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
<Button Click="Publish" Width="120">Publish</Button>
|
||||
</StackPanel>
|
||||
</PivotItem>
|
||||
|
||||
@@ -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<IMqttClientSessionStatus> _sessions = new ObservableCollection<IMqttClientSessionStatus>();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user