mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Removed 3rd-Party libraries.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
|
||||
@@ -28,8 +27,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
};
|
||||
|
||||
var factory = new MqttFactory();
|
||||
factory.GetLoggerFactory().AddConsole();
|
||||
|
||||
|
||||
var client = factory.CreateMqttClient();
|
||||
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
|
||||
@@ -14,10 +14,4 @@
|
||||
<ProjectReference Include="..\..\Frameworks\MQTTnet.Netstandard\MQTTnet.NetStandard.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console">
|
||||
<Version>1.1.2</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -4,8 +4,6 @@ using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.ManagedClient;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
@@ -16,14 +14,6 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttClient()
|
||||
.AddLogging()
|
||||
.BuildServiceProvider();
|
||||
|
||||
services.GetService<ILoggerFactory>()
|
||||
.AddConsole();
|
||||
|
||||
var ms = new ClientRetainedMessageHandler();
|
||||
|
||||
var options = new ManagedMqttClientOptions
|
||||
@@ -44,7 +34,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
try
|
||||
{
|
||||
var managedClient = services.GetRequiredService<ManagedMqttClient>();
|
||||
var managedClient = new MqttFactory().CreateManagedMqttClient();
|
||||
managedClient.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core;
|
||||
using MQTTnet.Core.Client;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
@@ -11,6 +8,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -18,46 +16,21 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttServer(options =>
|
||||
{
|
||||
|
||||
options.ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
if (p.Username != "USER" || p.Password != "PASS")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
};
|
||||
|
||||
options.DefaultCommunicationTimeout = TimeSpan.FromMinutes(10);
|
||||
})
|
||||
.AddMqttClient()
|
||||
.AddLogging()
|
||||
.BuildServiceProvider();
|
||||
|
||||
//services.GetService<ILoggerFactory>().AddConsole(LogLevel.Warning, true);
|
||||
|
||||
Console.WriteLine("Press 'c' for concurrent sends. Otherwise in one batch.");
|
||||
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);
|
||||
var server = Task.Factory.StartNew(async () => await RunServerAsync(), TaskCreationOptions.LongRunning);
|
||||
var client = Task.Factory.StartNew(async () => await RunClientAsync(2000, TimeSpan.FromMilliseconds(10), concurrent), TaskCreationOptions.LongRunning);
|
||||
|
||||
await Task.WhenAll(server, client).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static Task RunClientsAsync(int msgChunkSize, TimeSpan interval, IServiceProvider serviceProvider, bool concurrent)
|
||||
private static Task RunClientsAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
|
||||
{
|
||||
return Task.WhenAll(Enumerable.Range(0, 3).Select(i => Task.Run(() => RunClientAsync(msgChunkSize, interval, serviceProvider, concurrent))));
|
||||
return Task.WhenAll(Enumerable.Range(0, 3).Select(i => Task.Run(() => RunClientAsync(msgChunkSize, interval, concurrent))));
|
||||
}
|
||||
|
||||
private static async Task RunClientAsync(int msgChunkSize, TimeSpan interval, IServiceProvider serviceProvider, bool concurrent)
|
||||
private static async Task RunClientAsync(int msgChunkSize, TimeSpan interval, bool concurrent)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -69,7 +42,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
CommunicationTimeout = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
|
||||
var client = serviceProvider.GetRequiredService<IMqttClient>();
|
||||
var client = new MqttFactory().CreateMqttClient();
|
||||
|
||||
client.Connected += async (s, e) =>
|
||||
{
|
||||
@@ -77,7 +50,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
await client.SubscribeAsync(new List<TopicFilter>
|
||||
{
|
||||
new TopicFilter("#", MqttQualityOfServiceLevel.AtMostOnce)
|
||||
new TopicFilter("#")
|
||||
});
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
@@ -120,7 +93,6 @@ namespace MQTTnet.TestApp.NetCore
|
||||
stopwatch.Stop();
|
||||
Console.WriteLine($"Sent 10.000 messages within {stopwatch.ElapsedMilliseconds} ms ({stopwatch.ElapsedMilliseconds / (float)testMessageCount} ms / message).");
|
||||
|
||||
|
||||
var messages = new[] { message };
|
||||
var sentMessagesCount = 0;
|
||||
|
||||
@@ -158,9 +130,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
msgCount += msgs.Count;
|
||||
//send multiple
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
var now = DateTime.Now;
|
||||
if (last < now - TimeSpan.FromSeconds(1))
|
||||
{
|
||||
@@ -194,11 +164,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
return Task.Run(() => client.PublishAsync(applicationMessage));
|
||||
}
|
||||
|
||||
private static async Task RunServerAsync(IServiceProvider serviceProvider)
|
||||
private static async Task RunServerAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var mqttServer = serviceProvider.GetRequiredService<IMqttServer>();
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
var msgs = 0;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
mqttServer.ApplicationMessageReceived += (sender, args) =>
|
||||
@@ -211,7 +182,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
stopwatch.Restart();
|
||||
}
|
||||
};
|
||||
await mqttServer.StartAsync();
|
||||
await mqttServer.StartAsync(new MqttServerOptions());
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
@@ -7,7 +7,6 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -70,8 +69,6 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
{
|
||||
var factory = new MqttFactory();
|
||||
factory.GetLoggerFactory().AddConsole();
|
||||
|
||||
var client = factory.CreateMqttClient();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,24 +3,21 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MQTTnet.Diagnostics;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class ServerTest
|
||||
{
|
||||
public static Task RunAsync()
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttServer()
|
||||
.AddLogging();
|
||||
MqttNetConsoleTrace.ForwardToConsole();
|
||||
|
||||
services.Configure<MqttServerOptions>(options =>
|
||||
var options = new MqttServerOptions
|
||||
{
|
||||
options.ConnectionValidator = p =>
|
||||
ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
@@ -31,22 +28,19 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
};
|
||||
},
|
||||
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
|
||||
// Extend the timestamp for all messages from clients.
|
||||
options.ApplicationMessageInterceptor = context =>
|
||||
Storage = new RetainedMessageHandler(),
|
||||
ApplicationMessageInterceptor = context =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#"))
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
context.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
};
|
||||
// Protect several topics from being subscribed from every client.
|
||||
options.SubscriptionsInterceptor = context =>
|
||||
},
|
||||
SubscriptionsInterceptor = context =>
|
||||
{
|
||||
if (context.TopicFilter.Topic.StartsWith("admin/foo/bar") && context.ClientId != "theAdmin")
|
||||
{
|
||||
@@ -58,11 +52,11 @@ namespace MQTTnet.TestApp.NetCore
|
||||
context.AcceptSubscription = false;
|
||||
context.CloseConnection = true;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
serviceProvider.GetRequiredService<ILoggerFactory>().AddConsole();
|
||||
// Extend the timestamp for all messages from clients.
|
||||
// Protect several topics from being subscribed from every client.
|
||||
|
||||
//var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
|
||||
//options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
|
||||
@@ -70,18 +64,18 @@ namespace MQTTnet.TestApp.NetCore
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttFactory(serviceProvider).CreateMqttServer();
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
};
|
||||
|
||||
mqttServer.StartAsync();
|
||||
await mqttServer.StartAsync(options);
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
mqttServer.StopAsync();
|
||||
await mqttServer.StopAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -89,7 +83,6 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user