mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Add extension for RPC calls; replace locks; Refactored storage in Managed Client
This commit is contained in:
@@ -4,7 +4,6 @@ using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Packets;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Serializer;
|
||||
@@ -410,7 +409,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
using (var bodyStream = new MemoryStream(Join(buffer1), (int)headerStream.Position, header.BodyLength))
|
||||
{
|
||||
var deserializedPacket = serializer.Deserialize(new ReceivedMqttPacket(header, bodyStream));
|
||||
var deserializedPacket = serializer.Deserialize(header, bodyStream.ToArray());
|
||||
var buffer2 = serializer.Serialize(deserializedPacket);
|
||||
|
||||
Assert.AreEqual(expectedBase64Value, Convert.ToBase64String(Join(buffer2)));
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace MQTTnet.Core.Tests
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2", willMessage);
|
||||
|
||||
c1.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
await c1.SubscribeAsync(new TopicFilter("#"));
|
||||
await c1.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
|
||||
|
||||
await c2.DisconnectAsync();
|
||||
|
||||
@@ -167,7 +167,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
|
||||
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
await c2.SubscribeAsync(new TopicFilter("retained"));
|
||||
await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
|
||||
|
||||
await Task.Delay(500);
|
||||
}
|
||||
@@ -199,7 +199,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
|
||||
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
await c2.SubscribeAsync(new TopicFilter("retained"));
|
||||
await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
|
||||
|
||||
await Task.Delay(500);
|
||||
}
|
||||
@@ -277,7 +277,7 @@ namespace MQTTnet.Core.Tests
|
||||
|
||||
var c2 = await serverAdapter.ConnectTestClient(s, "c2");
|
||||
c2.ApplicationMessageReceived += (_, __) => receivedMessagesCount++;
|
||||
await c2.SubscribeAsync(new TopicFilter("retained"));
|
||||
await c2.SubscribeAsync(new TopicFilterBuilder().WithTopic("retained").Build());
|
||||
|
||||
await Task.Delay(500);
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ namespace MQTTnet.Core.Tests
|
||||
var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C"));
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp, "");
|
||||
sm.SubscribeAsync(sp, "").Wait();
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
@@ -24,7 +24,52 @@ namespace MQTTnet.Core.Tests
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsTrue(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
var result = sm.CheckSubscriptionsAsync(pp).Result;
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MqttSubscriptionsManager_SubscribeDifferentQoSSuccess()
|
||||
{
|
||||
var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
sm.SubscribeAsync(sp, "").Wait();
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
|
||||
};
|
||||
|
||||
var result = sm.CheckSubscriptionsAsync(pp).Result;
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtMostOnce);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MqttSubscriptionsManager_SubscribeTwoTimesSuccess()
|
||||
{
|
||||
var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C", MqttQualityOfServiceLevel.AtLeastOnce));
|
||||
|
||||
sm.SubscribeAsync(sp, "").Wait();
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce
|
||||
};
|
||||
|
||||
var result = sm.CheckSubscriptionsAsync(pp).Result;
|
||||
Assert.IsTrue(result.IsSubscribed);
|
||||
Assert.AreEqual(result.QualityOfServiceLevel, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -33,9 +78,9 @@ namespace MQTTnet.Core.Tests
|
||||
var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C"));
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp, "");
|
||||
sm.SubscribeAsync(sp, "").Wait();
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
@@ -43,7 +88,7 @@ namespace MQTTnet.Core.Tests
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -52,9 +97,9 @@ namespace MQTTnet.Core.Tests
|
||||
var sm = new MqttClientSubscriptionsManager(new MqttServerOptions());
|
||||
|
||||
var sp = new MqttSubscribePacket();
|
||||
sp.TopicFilters.Add(new TopicFilter("A/B/C"));
|
||||
sp.TopicFilters.Add(new TopicFilterBuilder().WithTopic("A/B/C").Build());
|
||||
|
||||
sm.Subscribe(sp, "");
|
||||
sm.SubscribeAsync(sp, "").Wait();
|
||||
|
||||
var pp = new MqttApplicationMessage
|
||||
{
|
||||
@@ -62,13 +107,13 @@ namespace MQTTnet.Core.Tests
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce
|
||||
};
|
||||
|
||||
Assert.IsTrue(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsTrue(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
|
||||
|
||||
var up = new MqttUnsubscribePacket();
|
||||
up.TopicFilters.Add("A/B/C");
|
||||
sm.Unsubscribe(up);
|
||||
sm.UnsubscribeAsync(up).Wait();
|
||||
|
||||
Assert.IsFalse(sm.CheckSubscriptions(pp).IsSubscribed);
|
||||
Assert.IsFalse(sm.CheckSubscriptionsAsync(pp).Result.IsSubscribed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -17,7 +18,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
CleanSession = true,
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "localhost"
|
||||
//Server = "localhost",
|
||||
Server = "192.168.1.174"
|
||||
},
|
||||
//ChannelOptions = new MqttClientWebSocketOptions
|
||||
//{
|
||||
@@ -78,6 +80,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
await client.SubscribeAsync(new TopicFilter("test", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
var applicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("A/B/C")
|
||||
.WithPayload("Hello World")
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<TargetFrameworks>netcoreapp2.0;net452;net461</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netcoreapp2.0|AnyCPU'">
|
||||
<DefineConstants>RELEASE;NETCOREAPP2_0</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -64,9 +63,17 @@ namespace MQTTnet.TestApp.NetCore
|
||||
sentMessagesCount++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second.");
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second. #1");
|
||||
|
||||
sentMessagesCount = 0;
|
||||
stopwatch.Restart();
|
||||
while (stopwatch.ElapsedMilliseconds < 1000)
|
||||
{
|
||||
await client.PublishAsync(messages).ConfigureAwait(false);
|
||||
sentMessagesCount++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second. #2");
|
||||
|
||||
var testMessageCount = 10000;
|
||||
for (var i = 0; i < testMessageCount; i++)
|
||||
@@ -141,8 +148,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
var msgs = 0;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
////var msgs = 0;
|
||||
////var stopwatch = Stopwatch.StartNew();
|
||||
////mqttServer.ApplicationMessageReceived += (sender, args) =>
|
||||
////{
|
||||
//// msgs++;
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
},
|
||||
|
||||
Storage = new RetainedMessageHandler(),
|
||||
|
||||
ApplicationMessageInterceptor = context =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#"))
|
||||
@@ -67,18 +68,29 @@ namespace MQTTnet.TestApp.NetCore
|
||||
mqttServer.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
MqttNetConsoleLogger.PrintToConsole(
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}'",
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])}'",
|
||||
ConsoleColor.Magenta);
|
||||
};
|
||||
|
||||
options.ApplicationMessageInterceptor = c =>
|
||||
{
|
||||
var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
var timestampProperty = content.Property("timestamp");
|
||||
if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
if (c.ApplicationMessage.Payload == null || c.ApplicationMessage.Payload.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
var timestampProperty = content.Property("timestamp");
|
||||
if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
{
|
||||
timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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