mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Linq;
|
|
using MQTTnet.Server;
|
|
|
|
namespace MQTTnet.AspNetCore
|
|
{
|
|
public static class ApplicationBuilderExtensions
|
|
{
|
|
public static IApplicationBuilder UseMqttEndpoint(this IApplicationBuilder app, string path = "/mqtt")
|
|
{
|
|
app.UseWebSockets();
|
|
app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.Path == path && context.WebSockets.IsWebSocketRequest)
|
|
{
|
|
string subprotocol = null;
|
|
|
|
if (context.Request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues)
|
|
&& requestedSubProtocolValues.Count > 0
|
|
&& requestedSubProtocolValues.Any(v => v.ToLower() == "mqtt")
|
|
)
|
|
{
|
|
subprotocol = "mqtt";
|
|
}
|
|
|
|
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
|
|
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subprotocol))
|
|
{
|
|
await adapter.AcceptWebSocketAsync(webSocket);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await next();
|
|
}
|
|
});
|
|
|
|
return app;
|
|
}
|
|
|
|
public static IApplicationBuilder UseMqttServer(this IApplicationBuilder app, Action<IMqttServer> configure)
|
|
{
|
|
var server = app.ApplicationServices.GetRequiredService<IMqttServer>();
|
|
|
|
configure(server);
|
|
|
|
return app;
|
|
}
|
|
}
|
|
}
|