mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 09:43:05 +08:00
Merge pull request #66 from JTrotta/develop
Add support to not persistent Queue
This commit is contained in:
@@ -19,26 +19,127 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Console.WriteLine("MQTTnet - TestApp.NetFramework");
|
||||
Console.WriteLine("1 = Start client");
|
||||
Console.WriteLine("2 = Start server");
|
||||
Console.WriteLine("3 = Start performance test");
|
||||
Console.WriteLine("2 = Start queued client");
|
||||
Console.WriteLine("3 = Start server");
|
||||
Console.WriteLine("4 = Start performance test");
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.Key == ConsoleKey.D1)
|
||||
{
|
||||
Task.Run(() => RunClientAsync(args));
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D2)
|
||||
if (pressedKey.Key == ConsoleKey.D2)
|
||||
{
|
||||
Task.Run(() => RunServerAsync(args));
|
||||
Task.Run(() => RunClientQueuedAsync(args));
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D3)
|
||||
{
|
||||
Task.Run(() => RunServerAsync(args));
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D4)
|
||||
{
|
||||
Task.Run(PerformanceTest.RunAsync);
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RunClientQueuedAsync(string[] arguments)
|
||||
{
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
|
||||
if (e.Exception != null)
|
||||
{
|
||||
Console.WriteLine(e.Exception);
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttClientManagedOptions
|
||||
{
|
||||
Server = "192.168.0.14",
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true,
|
||||
UserName = "lobu",
|
||||
Password = "passworda",
|
||||
KeepAlivePeriod = TimeSpan.FromSeconds(31),
|
||||
DefaultCommunicationTimeout = TimeSpan.FromSeconds(20),
|
||||
Storage = new TestStorage(),
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttManagedClient();
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
|
||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
|
||||
client.Connected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
await client.SubscribeAsync(new List<TopicFilter>
|
||||
{
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
|
||||
});
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
};
|
||||
|
||||
client.Disconnected += async (s, e) =>
|
||||
{
|
||||
Console.WriteLine("### DISCONNECTED FROM SERVER ###");
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("### RECONNECTING FAILED ###");
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(options);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine("### CONNECTING FAILED ###" + Environment.NewLine + exception);
|
||||
}
|
||||
|
||||
Console.WriteLine("### WAITING FOR APPLICATION MESSAGES ###");
|
||||
|
||||
int i = 0;
|
||||
while (true)
|
||||
{
|
||||
Console.ReadLine();
|
||||
i++;
|
||||
var applicationMessage = new MqttApplicationMessage(
|
||||
"PLNMAIN",
|
||||
Encoding.UTF8.GetBytes(string.Format("Hello World {0}", i)),
|
||||
MqttQualityOfServiceLevel.ExactlyOnce,
|
||||
false
|
||||
);
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RunClientAsync(string[] arguments)
|
||||
{
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
@@ -56,7 +157,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Uri = "localhost",
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true
|
||||
CleanSession = true,
|
||||
};
|
||||
|
||||
var client = new MqttClientFactory().CreateMqttClient();
|
||||
@@ -76,7 +177,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
await client.SubscribeAsync(new List<TopicFilter>
|
||||
{
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.ExactlyOnce)
|
||||
});
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
@@ -175,5 +276,70 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public sealed class TemporaryApplicationMessage
|
||||
{
|
||||
public TemporaryApplicationMessage(string topic, byte[] payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain)
|
||||
{
|
||||
Topic = topic ?? throw new ArgumentNullException(nameof(topic));
|
||||
Payload = payload ?? throw new ArgumentNullException(nameof(payload));
|
||||
QualityOfServiceLevel = qualityOfServiceLevel;
|
||||
Retain = retain;
|
||||
}
|
||||
|
||||
public string Topic { get; }
|
||||
|
||||
public byte[] Payload { get; }
|
||||
|
||||
public MqttQualityOfServiceLevel QualityOfServiceLevel { get; }
|
||||
|
||||
public bool Retain { get; }
|
||||
}
|
||||
|
||||
private class TestStorage : IMqttClientQueuedStorage
|
||||
{
|
||||
string serializationFile = System.IO.Path.Combine(Environment.CurrentDirectory, "messages.bin");
|
||||
private IList<MqttApplicationMessage> _messages = new List<MqttApplicationMessage>();
|
||||
|
||||
public Task<IList<MqttApplicationMessage>> LoadQueuedMessagesAsync()
|
||||
{
|
||||
//deserialize
|
||||
// MqttApplicationMessage is not serializable
|
||||
if (System.IO.File.Exists(serializationFile))
|
||||
{
|
||||
using (System.IO.Stream stream = System.IO.File.Open(serializationFile, System.IO.FileMode.Open))
|
||||
{
|
||||
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
|
||||
|
||||
var temp = (List<TemporaryApplicationMessage>)bformatter.Deserialize(stream);
|
||||
foreach (var a in temp)
|
||||
{
|
||||
_messages.Add(new MqttApplicationMessage(a.Topic, a.Payload, a.QualityOfServiceLevel, a.Retain));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Task.FromResult(_messages);
|
||||
}
|
||||
|
||||
public Task SaveQueuedMessagesAsync(IList<MqttApplicationMessage> messages)
|
||||
{
|
||||
_messages = messages;
|
||||
//serialize
|
||||
// MqttApplicationMessage is not serializable
|
||||
using (System.IO.Stream stream = System.IO.File.Open(serializationFile, System.IO.FileMode.Create))
|
||||
{
|
||||
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
|
||||
IList<TemporaryApplicationMessage> temp = new List<TemporaryApplicationMessage>();
|
||||
foreach (var m in _messages)
|
||||
{
|
||||
temp.Add(new TemporaryApplicationMessage(m.Topic, m.Payload, m.QualityOfServiceLevel, m.Retain));
|
||||
}
|
||||
bformatter.Serialize(stream, temp);
|
||||
}
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user