Преглед на файлове

Password Provider for dynamic password

release/3.x.x
Gerardo преди 7 години
родител
ревизия
29918fb454
променени са 5 файла, в които са добавени 59 реда и са изтрити 2 реда
  1. +2
    -0
      MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs
  2. +1
    -0
      MQTTnet.Core/ManagedClient/ManagedMqttClient.cs
  3. +3
    -0
      MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs
  4. +53
    -2
      Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs
  5. Двоични данни
     

+ 2
- 0
MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs Целия файл

@@ -10,5 +10,7 @@ namespace MQTTnet.Core.ManagedClient
TimeSpan AutoReconnectDelay { get; }

IManagedMqttClientStorage Storage { get; }

Func<IManagedMqttClientOptions, string> PasswordProvider { get; }
}
}

+ 1
- 0
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;
}


+ 3
- 0
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<IManagedMqttClientOptions, string> PasswordProvider { get; set; }

}
}

+ 53
- 2
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<ILoggerFactory>()
.AddConsole();

ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler();
Func<ManagedMqttClientOptions, string> 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<ManagedMqttClient>();
@@ -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<MqttApplicationMessage> messages)
{
File.WriteAllText(Filename, JsonConvert.SerializeObject(messages));
return Task.FromResult(0);
}

public Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync()
{
IList<MqttApplicationMessage> retainedMessages;
if (File.Exists(Filename))
{
var json = File.ReadAllText(Filename);
retainedMessages = JsonConvert.DeserializeObject<List<MqttApplicationMessage>>(json);
}
else
{
retainedMessages = new List<MqttApplicationMessage>();
}

return Task.FromResult(retainedMessages);
}
}
}
}

Двоични данни
Целия файл


Зареждане…
Отказ
Запис