diff --git a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs index 298de9a..c812ea9 100644 --- a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs @@ -10,5 +10,7 @@ namespace MQTTnet.Core.ManagedClient TimeSpan AutoReconnectDelay { get; } IManagedMqttClientStorage Storage { get; } + + Func PasswordProvider { get; } } } \ No newline at end of file diff --git a/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs b/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs index 6b2e1dd..0d37fe7 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs @@ -277,6 +277,7 @@ namespace MQTTnet.Core.ManagedClient try { + _options.PasswordProvider?.Invoke(_options); await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false); return ReconnectionResult.Reconnected; } diff --git a/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs b/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs index 2e24fd0..45b2af3 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs @@ -10,5 +10,8 @@ namespace MQTTnet.Core.ManagedClient public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5); public IManagedMqttClientStorage Storage { get; set; } + + public Func PasswordProvider { get; set; } + } } diff --git a/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs b/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs index b54de06..05dd0cf 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs @@ -7,6 +7,9 @@ using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.IO; +using Newtonsoft.Json; +using System.Collections.Generic; namespace MQTTnet.TestApp.NetCore { @@ -22,18 +25,32 @@ namespace MQTTnet.TestApp.NetCore services.GetService() .AddConsole(); + ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler(); + Func func = delegate (ManagedMqttClientOptions managedMqttClientOptions) + { + return "password"; + }; var options = new ManagedMqttClientOptions { ClientOptions = new MqttClientTcpOptions { Server = "broker.hivemq.com", - ClientId = "MQTTnetManagedClientTest" + ClientId = "MQTTnetManagedClientTest", + Password = "pippo", }, - AutoReconnectDelay = TimeSpan.FromSeconds(1) + AutoReconnectDelay = TimeSpan.FromSeconds(1), + Storage = ms, + PasswordProvider = o => + { + //o.ClientOptions.Password = GetPassword(); + return o.ClientOptions.Password; + } }; + + try { var managedClient = services.GetRequiredService(); @@ -59,5 +76,39 @@ namespace MQTTnet.TestApp.NetCore Console.WriteLine(e); } } + + + public static string GetPassword() + { + return "password"; + } + + + public class ClientRetainedMessageHandler : IManagedMqttClientStorage + { + private const string Filename = @"RetainedMessages.json"; + + public Task SaveQueuedMessagesAsync(IList messages) + { + File.WriteAllText(Filename, JsonConvert.SerializeObject(messages)); + return Task.FromResult(0); + } + + public Task> LoadQueuedMessagesAsync() + { + IList retainedMessages; + if (File.Exists(Filename)) + { + var json = File.ReadAllText(Filename); + retainedMessages = JsonConvert.DeserializeObject>(json); + } + else + { + retainedMessages = new List(); + } + + return Task.FromResult(retainedMessages); + } + } } } diff --git a/Tests/MQTTnet.TestApp.NetCore/messages.bin b/Tests/MQTTnet.TestApp.NetCore/messages.bin deleted file mode 100644 index 59505fa..0000000 Binary files a/Tests/MQTTnet.TestApp.NetCore/messages.bin and /dev/null differ