mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 17:23:04 +08:00
Add samples.
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace MQTTnet.Samples.Server;
|
||||||
|
|
||||||
|
public static class Server_Retained_Messages_Samples
|
||||||
|
{
|
||||||
|
public static async Task Persist_Retained_Messages()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* This sample starts a simple MQTT server which will store all retained messages in a file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var storePath = Path.Combine(Path.GetTempPath(), "RetainedMessages.json");
|
||||||
|
|
||||||
|
var mqttFactory = new MqttFactory();
|
||||||
|
|
||||||
|
// Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
|
||||||
|
var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder().WithDefaultEndpoint().Build();
|
||||||
|
|
||||||
|
using (var server = mqttFactory.CreateMqttServer(mqttServerOptions))
|
||||||
|
{
|
||||||
|
// Make sure that the server will load the retained messages.
|
||||||
|
server.LoadingRetainedMessageAsync += async eventArgs =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
eventArgs.LoadedRetainedMessages = await JsonSerializer.DeserializeAsync<List<MqttApplicationMessage>>(File.OpenRead(storePath));
|
||||||
|
Console.WriteLine("Retained messages loaded.");
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException)
|
||||||
|
{
|
||||||
|
// Ignore because nothing is stored yet.
|
||||||
|
Console.WriteLine("No retained messages stored yet.");
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make sure to persist the changed retained messages.
|
||||||
|
server.RetainedMessageChangedAsync += async eventArgs =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// This sample uses the property _StoredRetainedMessages_ which will contain all(!) retained messages.
|
||||||
|
// The event args also contain the affected retained message (property ChangedRetainedMessage). This can be
|
||||||
|
// used to write all retained messages to dedicated files etc. Then all files must be loaded and a full list
|
||||||
|
// of retained messages must be provided in the loaded event.
|
||||||
|
|
||||||
|
var buffer = JsonSerializer.SerializeToUtf8Bytes(eventArgs.StoredRetainedMessages);
|
||||||
|
await File.WriteAllBytesAsync(storePath, buffer);
|
||||||
|
Console.WriteLine("Retained messages saved.");
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
Console.WriteLine(exception);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Make sure to clear the retained messages when they are all deleted via API.
|
||||||
|
server.RetainedMessagesClearedAsync += _ =>
|
||||||
|
{
|
||||||
|
File.Delete(storePath);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
};
|
||||||
|
|
||||||
|
await server.StartAsync();
|
||||||
|
|
||||||
|
Console.WriteLine("Press Enter to exit.");
|
||||||
|
Console.ReadLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -128,11 +128,8 @@ public static class Server_Simple_Samples
|
|||||||
|
|
||||||
// Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
|
// Due to security reasons the "default" endpoint (which is unencrypted) is not enabled by default!
|
||||||
var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder().WithDefaultEndpoint().Build();
|
var mqttServerOptions = mqttFactory.CreateServerOptionsBuilder().WithDefaultEndpoint().Build();
|
||||||
|
|
||||||
var server = mqttFactory.CreateMqttServer(mqttServerOptions);
|
var server = mqttFactory.CreateMqttServer(mqttServerOptions);
|
||||||
|
|
||||||
await server.StartAsync();
|
await server.StartAsync();
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user