mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-27 00:40:01 +08:00
Added interceptor for application messages
This commit is contained in:
@@ -53,11 +53,13 @@ namespace MQTTnet.Core.Tests
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage(
|
||||
"My/last/will",
|
||||
Encoding.UTF8.GetBytes("Good byte."),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
true)
|
||||
WillMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "My/last/will",
|
||||
Payload = Encoding.UTF8.GetBytes("Good byte."),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Retain = true
|
||||
}
|
||||
};
|
||||
|
||||
SerializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
@@ -88,11 +90,13 @@ namespace MQTTnet.Core.Tests
|
||||
Username = "USER",
|
||||
KeepAlivePeriod = 123,
|
||||
CleanSession = true,
|
||||
WillMessage = new MqttApplicationMessage(
|
||||
"My/last/will",
|
||||
Encoding.UTF8.GetBytes("Good byte."),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
true)
|
||||
WillMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "My/last/will",
|
||||
Payload = Encoding.UTF8.GetBytes("Good byte."),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
Retain = true
|
||||
}
|
||||
};
|
||||
|
||||
DeserializeAndCompare(p, "EDUABE1RVFQE7gB7AANYWVoADE15L2xhc3Qvd2lsbAAKR29vZCBieXRlLgAEVVNFUgAEUEFTUw==");
|
||||
|
||||
@@ -151,12 +151,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
private static MqttApplicationMessage CreateMessage()
|
||||
{
|
||||
return new MqttApplicationMessage(
|
||||
"A/B/C",
|
||||
Encoding.UTF8.GetBytes("Hello World"),
|
||||
MqttQualityOfServiceLevel.AtMostOnce,
|
||||
false
|
||||
);
|
||||
return new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
Payload = Encoding.UTF8.GetBytes("Hello World"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
|
||||
};
|
||||
}
|
||||
|
||||
private static Task PublishSingleMessage(IMqttClient client, MqttApplicationMessage applicationMessage, ref int count)
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
else if (pressedKey.KeyChar == '2')
|
||||
{
|
||||
Task.Run(RunServerAsync);
|
||||
Task.Run(ServerTest.RunAsync);
|
||||
}
|
||||
else if (pressedKey.KeyChar == '3')
|
||||
{
|
||||
@@ -119,12 +119,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
var applicationMessage = new MqttApplicationMessage(
|
||||
"A/B/C",
|
||||
Encoding.UTF8.GetBytes("Hello World"),
|
||||
MqttQualityOfServiceLevel.AtLeastOnce,
|
||||
false
|
||||
);
|
||||
var applicationMessage = new MqttApplicationMessage
|
||||
{
|
||||
Topic = "A/B/C",
|
||||
Payload = Encoding.UTF8.GetBytes("Hello World"),
|
||||
QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce
|
||||
};
|
||||
|
||||
await client.PublishAsync(applicationMessage);
|
||||
}
|
||||
@@ -135,66 +135,6 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
}
|
||||
|
||||
private static Task RunServerAsync()
|
||||
{
|
||||
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
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
if (p.Username != "USER" || p.Password != "PASS")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
};
|
||||
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
|
||||
|
||||
//var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
|
||||
//options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
|
||||
//options.ConnectionBacklog = 5;
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
};
|
||||
|
||||
mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
mqttServer.StopAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
// ReSharper disable once UnusedMember.Local
|
||||
private static async void WikiCode()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Core.Diagnostics;
|
||||
using MQTTnet.Core.Protocol;
|
||||
using MQTTnet.Core.Server;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
public static class ServerTest
|
||||
{
|
||||
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
|
||||
{
|
||||
ConnectionValidator = p =>
|
||||
{
|
||||
if (p.ClientId == "SpecialClient")
|
||||
{
|
||||
if (p.Username != "USER" || p.Password != "PASS")
|
||||
{
|
||||
return MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
};
|
||||
|
||||
options.Storage = new RetainedMessageHandler();
|
||||
|
||||
options.ApplicationMessageInterceptor = message =>
|
||||
{
|
||||
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"));
|
||||
}
|
||||
};
|
||||
|
||||
//var certificate = new X509Certificate(@"C:\certs\test\test.cer", "");
|
||||
//options.TlsEndpointOptions.Certificate = certificate.Export(X509ContentType.Cert);
|
||||
//options.ConnectionBacklog = 5;
|
||||
//options.DefaultEndpointOptions.IsEnabled = true;
|
||||
//options.TlsEndpointOptions.IsEnabled = false;
|
||||
|
||||
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
};
|
||||
|
||||
mqttServer.StartAsync();
|
||||
|
||||
Console.WriteLine("Press any key to exit.");
|
||||
Console.ReadLine();
|
||||
|
||||
mqttServer.StopAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
Console.ReadLine();
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,11 +119,12 @@ namespace MQTTnet.TestApp.UniversalWindows
|
||||
payload = Convert.FromBase64String(Payload.Text);
|
||||
}
|
||||
|
||||
var message = new MqttApplicationMessage(
|
||||
Topic.Text,
|
||||
payload,
|
||||
qos,
|
||||
Retain.IsChecked == true);
|
||||
var message = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(Topic.Text)
|
||||
.WithPayload(payload)
|
||||
.WithQualityOfServiceLevel(qos)
|
||||
.WithRetainFlag(Retain.IsChecked == true)
|
||||
.Build();
|
||||
|
||||
await _mqttClient.PublishAsync(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user