mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-22 12:53:04 +08:00
Refactor options and add options builder
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class ClientTest
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true,
|
||||
////ChannelOptions = new MqttClientTcpOptions
|
||||
////{
|
||||
//// Server = "localhost"
|
||||
////},
|
||||
ChannelOptions = new MqttClientWebSocketOptions
|
||||
{
|
||||
Uri = "localhost"
|
||||
}
|
||||
};
|
||||
|
||||
var factory = new MqttFactory();
|
||||
factory.GetLoggerFactory().AddConsole();
|
||||
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
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 TopicFilterBuilder().WithTopic("#").Build());
|
||||
|
||||
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 ###");
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
var applicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("A/B/C")
|
||||
.WithPayload("Hello World")
|
||||
.WithAtLeastOnceQoS()
|
||||
.Build();
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,11 +28,14 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
var options = new ManagedMqttClientOptions
|
||||
{
|
||||
ClientOptions = new MqttClientTcpOptions
|
||||
ClientOptions = new MqttClientOptions
|
||||
{
|
||||
Server = "broker.hivemq.com",
|
||||
ClientId = "MQTTnetManagedClientTest",
|
||||
Credentials = new RandomPassword()
|
||||
Credentials = new RandomPassword(),
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "broker.hivemq.com"
|
||||
}
|
||||
},
|
||||
|
||||
AutoReconnectDelay = TimeSpan.FromSeconds(1),
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
@@ -20,7 +19,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttServer(options => {
|
||||
.AddMqttServer(options =>
|
||||
{
|
||||
|
||||
options.ConnectionValidator = p =>
|
||||
{
|
||||
@@ -41,11 +41,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
.AddLogging()
|
||||
.BuildServiceProvider();
|
||||
|
||||
services.GetService<ILoggerFactory>()
|
||||
.AddConsole(minLevel: LogLevel.Warning, includeScopes: true);
|
||||
|
||||
services.GetService<ILoggerFactory>().AddConsole(LogLevel.Warning, true);
|
||||
|
||||
Console.WriteLine("Press 'c' for concurrent sends. Otherwise in one batch.");
|
||||
var concurrent = Console.ReadKey(intercept: true).KeyChar == 'c';
|
||||
var concurrent = Console.ReadKey(true).KeyChar == 'c';
|
||||
|
||||
var server = Task.Factory.StartNew(() => RunServerAsync(services), TaskCreationOptions.LongRunning);
|
||||
var client = Task.Factory.StartNew(() => RunClientAsync(2000, TimeSpan.FromMilliseconds(10), services, concurrent), TaskCreationOptions.LongRunning);
|
||||
@@ -62,12 +61,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new MqttClientTcpOptions
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
Server = "localhost",
|
||||
ChannelOptions = new MqttClientTcpOptions { Server = "localhost" },
|
||||
ClientId = "Client1",
|
||||
CleanSession = true,
|
||||
DefaultCommunicationTimeout = TimeSpan.FromMinutes(10)
|
||||
CommunicationTimeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
|
||||
var client = serviceProvider.GetRequiredService<IMqttClient>();
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Packets;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
@@ -25,11 +20,11 @@ namespace MQTTnet.TestApp.NetCore
|
||||
Console.WriteLine("2 = Start server");
|
||||
Console.WriteLine("3 = Start performance test");
|
||||
Console.WriteLine("4 = Start managed client");
|
||||
|
||||
|
||||
var pressedKey = Console.ReadKey(true);
|
||||
if (pressedKey.KeyChar == '1')
|
||||
{
|
||||
Task.Run(RunClientAsync);
|
||||
Task.Run(ClientTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '2')
|
||||
{
|
||||
@@ -46,105 +41,22 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
Thread.Sleep(Timeout.Infinite);
|
||||
}
|
||||
|
||||
private static async Task RunClientAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttServer()
|
||||
.AddLogging()
|
||||
.BuildServiceProvider();
|
||||
|
||||
services.GetService<ILoggerFactory>()
|
||||
.AddConsole();
|
||||
|
||||
var options = new MqttClientWebSocketOptions
|
||||
{
|
||||
Uri = "localhost",
|
||||
ClientId = "XYZ",
|
||||
CleanSession = true
|
||||
};
|
||||
|
||||
var client = services.GetRequiredService<IMqttClient>();
|
||||
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 ###");
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
var applicationMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
Payload = Encoding.UTF8.GetBytes("Hello World"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
|
||||
};
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// This code is used at the Wiki on GitHub!
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
private static async void WikiCode()
|
||||
{
|
||||
{
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddMqttServer()
|
||||
.AddLogging()
|
||||
.BuildServiceProvider();
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
|
||||
var client = serviceProvider.GetRequiredService<IMqttClient>();
|
||||
var options = new MqttClientOptionsBuilder()
|
||||
.WithClientId("Client1")
|
||||
.WithTcpServer("broker.hivemq.com")
|
||||
.WithCredentials("bud", "%spencer%")
|
||||
.WithTls()
|
||||
.Build();
|
||||
|
||||
await client.ConnectAsync(options);
|
||||
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("MyTopic")
|
||||
@@ -157,9 +69,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
|
||||
{
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("/MQTTnet/is/awesome")
|
||||
.Build();
|
||||
var factory = new MqttFactory();
|
||||
factory.GetLoggerFactory().AddConsole();
|
||||
|
||||
var client = factory.CreateMqttClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user