Files
Web_IoTBase_Sever_Prod/IotMqttServer/Program.cs
2025-12-11 14:04:39 +08:00

61 lines
2.0 KiB
C#

using MQTTnet.Protocol;
using MQTTnet.Server;
namespace IotMqttServer
{
internal class Program
{
static async Task Main(string[] args)
{
var mqttServerFactory = new MqttServerFactory();
var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
using (var mqttServer = mqttServerFactory.CreateMqttServer(mqttServerOptions))
{
// Setup connection validation before starting the server so that there is
// no change to connect without valid credentials.
mqttServer.ValidatingConnectionAsync += e =>
{
//if (e.ClientId != "ValidClientId")
//{
// e.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
//}
//if (e.UserName != "ValidUser")
//{
// e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
//}
//if (e.Password != "SecretPassword")
//{
// e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
//}
return Task.CompletedTask;
};
mqttServer.ClientDisconnectedAsync += MqttServer_ClientDisconnectedAsync;
mqttServer.ClientConnectedAsync += MqttServer_ClientConnectedAsync;
await mqttServer.StartAsync();
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();
await mqttServer.StopAsync();
}
Console.WriteLine("Hello, World!");
}
private static Task MqttServer_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg)
{
throw new NotImplementedException();
}
private static Task MqttServer_ClientConnectedAsync(ClientConnectedEventArgs arg)
{
throw new NotImplementedException();
}
}
}