mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-28 09:12:37 +08:00
Add more function mappings for MQTTnet Server.
This commit is contained in:
@@ -2,7 +2,10 @@
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
namespace MQTTnet.TestApp.NetCore
|
||||
@@ -25,7 +28,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
}
|
||||
};
|
||||
|
||||
client.ApplicationMessageReceived += (s, e) =>
|
||||
client.ApplicationMessageReceivedHandler = new MqttApplicationMessageHandlerDelegate(e =>
|
||||
{
|
||||
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
|
||||
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
|
||||
@@ -33,18 +36,18 @@ namespace MQTTnet.TestApp.NetCore
|
||||
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
|
||||
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
|
||||
Console.WriteLine();
|
||||
};
|
||||
});
|
||||
|
||||
client.Connected += async (s, e) =>
|
||||
client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(async e =>
|
||||
{
|
||||
Console.WriteLine("### CONNECTED WITH SERVER ###");
|
||||
|
||||
await client.SubscribeAsync(new TopicFilterBuilder().WithTopic("#").Build());
|
||||
|
||||
Console.WriteLine("### SUBSCRIBED ###");
|
||||
};
|
||||
});
|
||||
|
||||
client.Disconnected += async (s, e) =>
|
||||
client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(async e =>
|
||||
{
|
||||
Console.WriteLine("### DISCONNECTED FROM SERVER ###");
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
@@ -57,7 +60,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Console.WriteLine("### RECONNECTING FAILED ###");
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Extensions.ManagedClient;
|
||||
using MQTTnet.Protocol;
|
||||
|
||||
@@ -34,10 +35,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
try
|
||||
{
|
||||
var managedClient = new MqttFactory().CreateManagedMqttClient();
|
||||
managedClient.ApplicationMessageReceived += (s, e) =>
|
||||
managedClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageHandlerDelegate(e =>
|
||||
{
|
||||
Console.WriteLine(">> RECEIVED: " + e.ApplicationMessage.Topic);
|
||||
};
|
||||
});
|
||||
|
||||
await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("1"));
|
||||
await managedClient.PublishAsync(builder => builder.WithTopic("Step").WithPayload("2").WithAtLeastOnceQoS());
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Formatter;
|
||||
using MQTTnet.Protocol;
|
||||
using Newtonsoft.Json;
|
||||
@@ -80,7 +81,7 @@ namespace MQTTnet.TestApp.NetCore
|
||||
var topic = Guid.NewGuid().ToString();
|
||||
|
||||
MqttApplicationMessage receivedMessage = null;
|
||||
client.ApplicationMessageReceived += (s, e) => receivedMessage = e.ApplicationMessage;
|
||||
client.ApplicationMessageReceivedHandler = new MqttApplicationMessageHandlerDelegate(e => receivedMessage = e.ApplicationMessage);
|
||||
|
||||
await client.ConnectAsync(options);
|
||||
await client.SubscribeAsync(topic, MqttQualityOfServiceLevel.AtLeastOnce);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Protocol;
|
||||
using MQTTnet.Server;
|
||||
|
||||
@@ -78,12 +79,12 @@ namespace MQTTnet.TestApp.NetCore
|
||||
|
||||
var mqttServer = new MqttFactory().CreateMqttServer();
|
||||
|
||||
mqttServer.ApplicationMessageReceived += (s, e) =>
|
||||
mqttServer.ApplicationMessageReceivedHandler = new MqttApplicationMessageHandlerDelegate(e =>
|
||||
{
|
||||
MqttNetConsoleLogger.PrintToConsole(
|
||||
$"'{e.ClientId}' reported '{e.ApplicationMessage.Topic}' > '{Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])}'",
|
||||
ConsoleColor.Magenta);
|
||||
};
|
||||
});
|
||||
|
||||
//options.ApplicationMessageInterceptor = c =>
|
||||
//{
|
||||
@@ -107,10 +108,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
// }
|
||||
//};
|
||||
|
||||
mqttServer.ClientDisconnected += (s, e) =>
|
||||
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e =>
|
||||
{
|
||||
Console.Write("Client disconnected event fired.");
|
||||
};
|
||||
});
|
||||
|
||||
await mqttServer.StartAsync(options);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user