mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
Add extension for RPC calls; replace locks; Refactored storage in Managed Client
This commit is contained in:
@@ -127,6 +127,10 @@
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Extensions\MQTTnet.Extensions.Rpc\MQTTnet.Extensions.Rpc.csproj">
|
||||
<Project>{c444e9c8-95fa-430e-9126-274129de16cd}</Project>
|
||||
<Name>MQTTnet.Extensions.Rpc</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.Netstandard\MQTTnet.NetStandard.csproj">
|
||||
<Project>{3587e506-55a2-4eb3-99c7-dc01e42d25d2}</Project>
|
||||
<Name>MQTTnet.NetStandard</Name>
|
||||
|
||||
@@ -65,6 +65,40 @@
|
||||
<Button Click="Publish" Width="120">Publish</Button>
|
||||
</StackPanel>
|
||||
</PivotItem>
|
||||
<PivotItem Header="Execute RPC">
|
||||
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<TextBlock>Method:</TextBlock>
|
||||
<TextBox x:Name="RpcMethod"></TextBox>
|
||||
|
||||
<TextBlock>Payload:</TextBlock>
|
||||
<TextBox x:Name="RpcPayload"></TextBox>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton x:Name="RpcText" IsChecked="True" GroupName="payload">Text</RadioButton>
|
||||
<RadioButton x:Name="RpcBase64" GroupName="payload">Base64</RadioButton>
|
||||
</StackPanel>
|
||||
|
||||
<TextBlock>QoS:</TextBlock>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton Margin="0,0,10,0" x:Name="RpcQoS0" GroupName="qos" IsChecked="True">0 (At most once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="RpcQoS1" GroupName="qos">1 (At least once)</RadioButton>
|
||||
<RadioButton Margin="0,0,10,0" x:Name="RpcQoS2" GroupName="qos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
|
||||
<TextBlock>Responses:</TextBlock>
|
||||
<ListBox MinHeight="50" MaxHeight="250" x:Name="RpcResponses" Margin="0,0,0,10">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<ContentPresenter Content="{Binding}" FontFamily="Consolas" FontSize="12"></ContentPresenter>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Click="ExecuteRpc" Width="120" Margin="0,0,10,0">Execute</Button>
|
||||
<Button Click="ClearRpcResponses" Width="200">Clear responses</Button>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</PivotItem>
|
||||
<PivotItem Header="Subscribe">
|
||||
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<TextBlock>Topic:</TextBlock>
|
||||
@@ -86,7 +120,7 @@
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Click="Subscribe" Width="120" Margin="0,0,10,0">Subscribe</Button>
|
||||
<Button Click="Unsubscribe" Width="120" Margin="0,0,10,0">Unsubscribe</Button>
|
||||
@@ -101,7 +135,7 @@
|
||||
|
||||
<CheckBox x:Name="ServerPersistRetainedMessages" IsChecked="True">Persist retained messages in JSON format</CheckBox>
|
||||
<CheckBox x:Name="ServerClearRetainedMessages">Clear previously retained messages on startup</CheckBox>
|
||||
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Width="120" Margin="0,0,10,0" Click="StartServer">Start</Button>
|
||||
<Button Width="120" Margin="0,0,10,0" Click="StopServer">Stop</Button>
|
||||
|
||||
@@ -7,6 +7,8 @@ using Windows.UI.Core;
|
||||
using Windows.UI.Xaml;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Exceptions;
|
||||
using MQTTnet.Extensions.Rpc;
|
||||
using MQTTnet.Implementations;
|
||||
using MQTTnet.ManagedClient;
|
||||
using MQTTnet.Protocol;
|
||||
@@ -245,6 +247,95 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
|
||||
// This code is for the Wiki at GitHub!
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
|
||||
private async void StartServer(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_mqttServer != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
JsonServerStorage storage = null;
|
||||
if (ServerPersistRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage = new JsonServerStorage();
|
||||
|
||||
if (ServerClearRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
_mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
var options = new MqttServerOptions();
|
||||
options.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
|
||||
options.Storage = storage;
|
||||
|
||||
await _mqttServer.StartAsync(options);
|
||||
}
|
||||
|
||||
private async void StopServer(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_mqttServer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _mqttServer.StopAsync();
|
||||
_mqttServer = null;
|
||||
}
|
||||
|
||||
private void ClearReceivedMessages(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ReceivedMessages.Items.Clear();
|
||||
}
|
||||
|
||||
private async void ExecuteRpc(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var qos = MqttQualityOfServiceLevel.AtMostOnce;
|
||||
if (RpcQoS1.IsChecked == true)
|
||||
{
|
||||
qos = MqttQualityOfServiceLevel.AtLeastOnce;
|
||||
}
|
||||
|
||||
if (RpcQoS2.IsChecked == true)
|
||||
{
|
||||
qos = MqttQualityOfServiceLevel.ExactlyOnce;
|
||||
}
|
||||
|
||||
var payload = new byte[0];
|
||||
if (RpcText.IsChecked == true)
|
||||
{
|
||||
payload = Encoding.UTF8.GetBytes(RpcPayload.Text);
|
||||
}
|
||||
|
||||
if (RpcBase64.IsChecked == true)
|
||||
{
|
||||
payload = Convert.FromBase64String(RpcPayload.Text);
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var rpcClient = new MqttRpcClient(_mqttClient);
|
||||
await rpcClient.EnableAsync();
|
||||
var response = await rpcClient.ExecuteAsync(TimeSpan.FromSeconds(5), RpcMethod.Text, payload, qos);
|
||||
await rpcClient.DisableAsync();
|
||||
|
||||
RpcResponses.Items.Add(RpcMethod.Text + " >>> " + Encoding.UTF8.GetString(response));
|
||||
}
|
||||
catch (MqttCommunicationTimedOutException)
|
||||
{
|
||||
RpcResponses.Items.Add(RpcMethod.Text + " >>> [TIMEOUT]");
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearRpcResponses(object sender, RoutedEventArgs e)
|
||||
{
|
||||
RpcResponses.Items.Clear();
|
||||
}
|
||||
|
||||
private async Task WikiCode()
|
||||
{
|
||||
{
|
||||
@@ -293,9 +384,9 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
// Use secure TCP connection.
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithTcpServer("broker.hivemq.com")
|
||||
.WithTls()
|
||||
.Build();
|
||||
.WithTcpServer("broker.hivemq.com")
|
||||
.WithTls()
|
||||
.Build();
|
||||
}
|
||||
|
||||
{
|
||||
@@ -480,48 +571,5 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private async void StartServer(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_mqttServer != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
JsonServerStorage storage = null;
|
||||
if (ServerPersistRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage = new JsonServerStorage();
|
||||
|
||||
if (ServerClearRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
_mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
var options = new MqttServerOptions();
|
||||
options.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
|
||||
options.Storage = storage;
|
||||
|
||||
await _mqttServer.StartAsync(options);
|
||||
}
|
||||
|
||||
private async void StopServer(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_mqttServer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _mqttServer.StopAsync();
|
||||
_mqttServer = null;
|
||||
}
|
||||
|
||||
private void ClearReceivedMessages(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ReceivedMessages.Items.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user