mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-18 02:13:00 +08:00
Add extension for RPC calls; replace locks; Refactored storage in Managed Client
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
@@ -17,7 +18,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
CleanSession = true,
|
||||
ChannelOptions = new MqttClientTcpOptions
|
||||
{
|
||||
Server = "localhost"
|
||||
//Server = "localhost",
|
||||
Server = "192.168.1.174"
|
||||
},
|
||||
//ChannelOptions = new MqttClientWebSocketOptions
|
||||
//{
|
||||
@@ -78,6 +80,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Console.ReadLine();
|
||||
|
||||
await client.SubscribeAsync(new TopicFilter("test", MqttQualityOfServiceLevel.AtMostOnce));
|
||||
|
||||
var applicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic("A/B/C")
|
||||
.WithPayload("Hello World")
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<TargetFrameworks>netcoreapp2.0;net452;net461</TargetFrameworks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netcoreapp2.0|AnyCPU'">
|
||||
<DefineConstants>RELEASE;NETCOREAPP2_0</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -64,9 +63,17 @@ namespace MQTTnet.TestApp.NetCore
|
||||
sentMessagesCount++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second.");
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second. #1");
|
||||
|
||||
sentMessagesCount = 0;
|
||||
stopwatch.Restart();
|
||||
while (stopwatch.ElapsedMilliseconds < 1000)
|
||||
{
|
||||
await client.PublishAsync(messages).ConfigureAwait(false);
|
||||
sentMessagesCount++;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Sending {sentMessagesCount} messages per second. #2");
|
||||
|
||||
var testMessageCount = 10000;
|
||||
for (var i = 0; i < testMessageCount; i++)
|
||||
@@ -141,8 +148,8 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
var msgs = 0;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
////var msgs = 0;
|
||||
////var stopwatch = Stopwatch.StartNew();
|
||||
////mqttServer.ApplicationMessageReceived += (sender, args) =>
|
||||
////{
|
||||
//// msgs++;
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
},
|
||||
|
||||
Storage = new RetainedMessageHandler(),
|
||||
|
||||
ApplicationMessageInterceptor = context =>
|
||||
{
|
||||
if (MqttTopicFilterComparer.IsMatch(context.ApplicationMessage.Topic, "/myTopic/WithTimestamp/#"))
|
||||
@@ -67,18 +68,29 @@ namespace MQTTnet.TestApp.NetCore
|
||||
mqttServer.ApplicationMessageReceived += (s, e) =>
|
||||
{
|
||||
MqttNetConsoleLogger.PrintToConsole(
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}'",
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])}'",
|
||||
ConsoleColor.Magenta);
|
||||
};
|
||||
|
||||
options.ApplicationMessageInterceptor = c =>
|
||||
{
|
||||
var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
var timestampProperty = content.Property("timestamp");
|
||||
if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
if (c.ApplicationMessage.Payload == null || c.ApplicationMessage.Payload.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var content = JObject.Parse(Encoding.UTF8.GetString(c.ApplicationMessage.Payload));
|
||||
var timestampProperty = content.Property("timestamp");
|
||||
if (timestampProperty != null && timestampProperty.Value.Type == JTokenType.Null)
|
||||
{
|
||||
timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
timestampProperty.Value = DateTime.Now.ToString("O");
|
||||
c.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(content.ToString());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user