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

78 lines
2.2 KiB
C#

using MQTTnet.Server;
namespace iotsystem
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.MapControllers();
app.Run();
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.ReadLine();
await mqttServer.StopAsync();
}
}
private static Task MqttServer_ClientConnectedAsync(ClientConnectedEventArgs arg)
{
throw new NotImplementedException();
}
private static Task MqttServer_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg)
{
throw new NotImplementedException();
}
}
}