mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-25 07:41:32 +08:00
Optimized persisting of retained messages to avoid useless writes to the filesystem
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Server;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MQTTnet.TestApp.UniversalWindows
|
||||
{
|
||||
public class JsonServerStorage : IMqttServerStorage
|
||||
{
|
||||
private readonly string _filename = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Retained.json");
|
||||
|
||||
public async Task SaveRetainedMessagesAsync(IList<MqttApplicationMessage> messages)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
var json = JsonConvert.SerializeObject(messages);
|
||||
File.WriteAllText(_filename, json);
|
||||
}
|
||||
|
||||
public async Task<IList<MqttApplicationMessage>> LoadRetainedMessagesAsync()
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
if (!File.Exists(_filename))
|
||||
{
|
||||
return new List<MqttApplicationMessage>();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var json = File.ReadAllText(_filename);
|
||||
return JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<MqttApplicationMessage>();
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (File.Exists(_filename))
|
||||
{
|
||||
File.Delete(_filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,7 @@
|
||||
<Compile Include="App.xaml.cs">
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="JsonServerStorage.cs" />
|
||||
<Compile Include="MainPage.xaml.cs">
|
||||
<DependentUpon>MainPage.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
<TextBox x:Name="ClientId"></TextBox>
|
||||
<TextBlock>Clean session:</TextBlock>
|
||||
<CheckBox x:Name="CleanSession" IsChecked="True"></CheckBox>
|
||||
|
||||
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<RadioButton x:Name="UseTcp" IsChecked="True" GroupName="connection">TCP</RadioButton>
|
||||
<RadioButton x:Name="UseWs" GroupName="connection">WS</RadioButton>
|
||||
@@ -77,9 +77,19 @@
|
||||
<RadioButton Margin="0,0,10,0" x:Name="SubscribeQoS2" GroupName="sqos">2 (Exactly once)</RadioButton>
|
||||
</StackPanel>
|
||||
|
||||
<TextBlock>Received messages:</TextBlock>
|
||||
<ListBox MinHeight="50" MaxHeight="250" x:Name="ReceivedMessages" 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="Subscribe" Width="120" Margin="0,0,10,0">Subscribe</Button>
|
||||
<Button Click="Unsubscribe" Width="120">Unsubscribe</Button>
|
||||
<Button Click="Unsubscribe" Width="120" Margin="0,0,10,0">Unsubscribe</Button>
|
||||
<Button Click="ClearReceivedMessages" Width="200">Clear received messages</Button>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</PivotItem>
|
||||
@@ -88,6 +98,9 @@
|
||||
<TextBlock>Port:</TextBlock>
|
||||
<TextBox x:Name="ServerPort" Text="1883"></TextBox>
|
||||
|
||||
<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>
|
||||
|
||||
@@ -88,10 +88,13 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
if (_mqttClient != null)
|
||||
{
|
||||
await _mqttClient.DisconnectAsync();
|
||||
_mqttClient.ApplicationMessageReceived -= OnApplicationMessageReceived;
|
||||
}
|
||||
|
||||
var factory = new MqttFactory();
|
||||
_mqttClient = factory.CreateMqttClient();
|
||||
_mqttClient.ApplicationMessageReceived += OnApplicationMessageReceived;
|
||||
|
||||
await _mqttClient.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -100,6 +103,17 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
}
|
||||
}
|
||||
|
||||
private async void OnApplicationMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
|
||||
{
|
||||
var item = $"Timestamp: {DateTime.Now:O} | Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {Encoding.UTF8.GetString(eventArgs.ApplicationMessage.Payload)} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel}";
|
||||
|
||||
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
|
||||
{
|
||||
ReceivedMessages.Items.Add(item);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private async void Publish(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (_mqttClient == null)
|
||||
@@ -332,9 +346,21 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
return;
|
||||
}
|
||||
|
||||
JsonServerStorage storage = null;
|
||||
if (ServerPersistRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage = new JsonServerStorage();
|
||||
|
||||
if (ServerClearRetainedMessages.IsChecked == true)
|
||||
{
|
||||
storage.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
_mqttServer = new MqttFactory().CreateMqttServer(o =>
|
||||
{
|
||||
o.DefaultEndpointOptions.Port = int.Parse(ServerPort.Text);
|
||||
o.Storage = storage;
|
||||
});
|
||||
|
||||
await _mqttServer.StartAsync();
|
||||
@@ -350,5 +376,10 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
await _mqttServer.StopAsync();
|
||||
_mqttServer = null;
|
||||
}
|
||||
|
||||
private void ClearReceivedMessages(object sender, RoutedEventArgs e)
|
||||
{
|
||||
ReceivedMessages.Items.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user