mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Refactor manage client
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.ManagedClient;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class ManagedClientTest
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
MqttNetTrace.TraceMessagePublished += (s, e) =>
|
||||
{
|
||||
Console.WriteLine($">> [{e.TraceMessage.Timestamp:O}] [{e.TraceMessage.ThreadId}] [{e.TraceMessage.Source}] [{e.TraceMessage.Level}]: {e.TraceMessage.Message}");
|
||||
if (e.TraceMessage.Exception != null)
|
||||
{
|
||||
Console.WriteLine(e.TraceMessage.Exception);
|
||||
}
|
||||
};
|
||||
|
||||
var options = new ManagedMqttClientOptions
|
||||
{
|
||||
ClientOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "broker.hivemq.com",
|
||||
ClientId = "MQTTnetManagedClientTest"
|
||||
},
|
||||
|
||||
AutoReconnectDelay = TimeSpan.FromSeconds(1)
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var managedClient = new MqttClientFactory().CreateManagedMqttClient();
|
||||
managedClient.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
|
||||
};
|
||||
|
||||
await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("1").Build());
|
||||
await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS().Build());
|
||||
|
||||
await managedClient.StartAsync(options);
|
||||
|
||||
await managedClient.SubscribeAsync(new TopicFilter("xyz", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
await managedClient.EnqueueAsync(new MqttApplicationMessageBuilder().WithTopic("Step").WithPayload("3").Build());
|
||||
|
||||
Console.WriteLine("Managed client started.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,22 +23,27 @@ namespace MQTTnet.TestApp.NetCore
|
||||
Console.WriteLine("1 = Start client");
|
||||
Console.WriteLine("2 = Start server");
|
||||
Console.WriteLine("3 = Start performance test");
|
||||
Console.WriteLine("4 = Start managed client");
|
||||
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.Key == ConsoleKey.D1)
|
||||
if (pressedKey.KeyChar == '1')
|
||||
{
|
||||
Task.Run(RunClientAsync);
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D2)
|
||||
else if (pressedKey.KeyChar == '2')
|
||||
{
|
||||
Task.Run(() => RunServerAsync());
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
Task.Run(RunServerAsync);
|
||||
}
|
||||
else if (pressedKey.Key == ConsoleKey.D3)
|
||||
else if (pressedKey.KeyChar == '3')
|
||||
{
|
||||
Task.Run(PerformanceTest.RunAsync);
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '4')
|
||||
{
|
||||
Task.Run(ManagedClientTest.RunAsync);
|
||||
}
|
||||
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
|
||||
private static async Task RunClientAsync()
|
||||
|
||||
Reference in New Issue
Block a user