提交 13762eed 编写于 作者: C Christian

Update Server_Asp_Net_Samples.cs

上级 e3349501
...@@ -5,12 +5,15 @@ ...@@ -5,12 +5,15 @@
// ReSharper disable UnusedType.Global // ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
// ReSharper disable InconsistentNaming // ReSharper disable InconsistentNaming
// ReSharper disable EmptyConstructor
// ReSharper disable MemberCanBeMadeStatic.Local
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using MQTTnet.AspNetCore; using MQTTnet.AspNetCore;
using MQTTnet.Server;
namespace MQTTnet.Samples.Server; namespace MQTTnet.Samples.Server;
...@@ -30,7 +33,7 @@ public static class Server_Asp_Net_Samples ...@@ -30,7 +33,7 @@ public static class Server_Asp_Net_Samples
{ {
// This will allow MQTT connections based on TCP port 1883. // This will allow MQTT connections based on TCP port 1883.
o.ListenAnyIP(1883, l => l.UseMqtt()); o.ListenAnyIP(1883, l => l.UseMqtt());
// This will allow MQTT connections based on HTTP WebSockets with URI "localhost:5000/mqtt" // This will allow MQTT connections based on HTTP WebSockets with URI "localhost:5000/mqtt"
// See code below for URI configuration. // See code below for URI configuration.
o.ListenAnyIP(5000); // Default HTTP pipeline o.ListenAnyIP(5000); // Default HTTP pipeline
...@@ -42,9 +45,30 @@ public static class Server_Asp_Net_Samples ...@@ -42,9 +45,30 @@ public static class Server_Asp_Net_Samples
return host.RunConsoleAsync(); return host.RunConsoleAsync();
} }
sealed class MqttController
{
public MqttController()
{
// Inject other services via constructor.
}
public Task OnClientConnected(ClientConnectedEventArgs eventArgs)
{
Console.WriteLine($"Client '{eventArgs.ClientId}' connected.");
return Task.CompletedTask;
}
public Task ValidateConnection(ValidatingConnectionEventArgs eventArgs)
{
Console.WriteLine($"Client '{eventArgs.ClientId}' wants to connect. Accepting!");
return Task.CompletedTask;
}
}
sealed class Startup sealed class Startup
{ {
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment environment, MqttController mqttController)
{ {
app.UseRouting(); app.UseRouting();
...@@ -64,17 +88,8 @@ public static class Server_Asp_Net_Samples ...@@ -64,17 +88,8 @@ public static class Server_Asp_Net_Samples
* Attach event handlers etc. if required. * Attach event handlers etc. if required.
*/ */
server.ValidatingConnectionAsync += e => server.ValidatingConnectionAsync += mqttController.ValidateConnection;
{ server.ClientConnectedAsync += mqttController.OnClientConnected;
Console.WriteLine($"Client '{e.ClientId}' wants to connect. Accepting!");
return Task.CompletedTask;
};
server.ClientConnectedAsync += e =>
{
Console.WriteLine($"Client '{e.ClientId}' connected.");
return Task.CompletedTask;
};
}); });
} }
...@@ -88,6 +103,8 @@ public static class Server_Asp_Net_Samples ...@@ -88,6 +103,8 @@ public static class Server_Asp_Net_Samples
services.AddMqttConnectionHandler(); services.AddMqttConnectionHandler();
services.AddConnections(); services.AddConnections();
services.AddSingleton<MqttController>();
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册