mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
initial version
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -11,20 +12,15 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static 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);
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var options = new MqttServerOptions
|
||||
var services = new ServiceCollection()
|
||||
.AddMqttServer()
|
||||
.AddLogging();
|
||||
|
||||
services.Configure<MqttServerOptions>(options =>
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
options.ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
@@ -35,22 +31,24 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
|
||||
options.ApplicationMessageInterceptor = message =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.IsMatch(message.Topic, "/myTopic/WithTimestamp/#"))
|
||||
options.ApplicationMessageInterceptor = message =>
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
message.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
if (MqttTopicFilterComparer.IsMatch(message.Topic, "/myTopic/WithTimestamp/#"))
|
||||
{
|
||||
// Replace the payload with the timestamp. But also extending a JSON
|
||||
// based payload with the timestamp is a suitable use case.
|
||||
message.Payload = Encoding.UTF8.GetBytes(DateTime.Now.ToString("O"));
|
||||
}
|
||||
|
||||
return message;
|
||||
};
|
||||
return message;
|
||||
};
|
||||
});
|
||||
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
//var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
|
||||
//options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
|
||||
@@ -58,7 +56,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
var mqttServer = serviceProvider.GetRequiredService<IMqttServer>();
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
|
||||
Reference in New Issue
Block a user