initial version

This commit is contained in:
JanEggers
2017-10-22 19:22:14 +02:00
parent 41edc3a1ec
commit d10f29bbc6
42 changed files with 580 additions and 487 deletions
+23 -25
View File
@@ -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.");