mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-16 01:03:01 +08:00
56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using System;
|
|
using MQTTnet.Core.Diagnostics;
|
|
using MQTTnet.Core.Protocol;
|
|
using MQTTnet.Core.Server;
|
|
|
|
namespace MQTTnet.TestMqttServer
|
|
{
|
|
public static class Program
|
|
{
|
|
public static void Main(string[] arguments)
|
|
{
|
|
MqttTrace.TraceMessagePublished += (s, e) =>
|
|
{
|
|
Console.WriteLine($">> [{e.ThreadId}] [{e.Source}] [{e.Level}]: {e.Message}");
|
|
if (e.Exception != null)
|
|
{
|
|
Console.WriteLine(e.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;
|
|
}
|
|
};
|
|
|
|
var mqttServer = new MqttServerFactory().CreateMqttServer(options);
|
|
mqttServer.Start();
|
|
|
|
Console.WriteLine("Press any key to exit.");
|
|
Console.ReadLine();
|
|
|
|
mqttServer.Stop();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
|
|
Console.ReadLine();
|
|
}
|
|
}
|
|
}
|