mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 11:03:07 +08:00
fixed websocket subprotocol negotiation
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using MQTTnet.Core.Server;
|
using MQTTnet.Core.Server;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace MQTTnet.AspNetCore
|
namespace MQTTnet.AspNetCore
|
||||||
{
|
{
|
||||||
@@ -14,8 +15,21 @@ namespace MQTTnet.AspNetCore
|
|||||||
{
|
{
|
||||||
if (context.Request.Path == path && context.WebSockets.IsWebSocketRequest)
|
if (context.Request.Path == path && context.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
|
string subprotocol = null;
|
||||||
|
|
||||||
|
if (context.Request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
|
||||||
|
{
|
||||||
|
if (requestedSubProtocolValues.Count > 0)
|
||||||
|
{
|
||||||
|
if (requestedSubProtocolValues.Any(v => v.ToLower() == "mqtt"))
|
||||||
|
{
|
||||||
|
subprotocol = "mqtt";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
|
var adapter = app.ApplicationServices.GetRequiredService<MqttWebSocketServerAdapter>();
|
||||||
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync("mqtt"))
|
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subprotocol))
|
||||||
{
|
{
|
||||||
await adapter.AcceptWebSocketAsync(webSocket);
|
await adapter.AcceptWebSocketAsync(webSocket);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ namespace MQTTnet.TestApp.NetCore
|
|||||||
{
|
{
|
||||||
Server = "localhost"
|
Server = "localhost"
|
||||||
},
|
},
|
||||||
////ChannelOptions = new MqttClientWebSocketOptions
|
//ChannelOptions = new MqttClientWebSocketOptions
|
||||||
////{
|
//{
|
||||||
//// Uri = "localhost"
|
// Uri = "ws://localhost:59690/mqtt"
|
||||||
////}
|
//}
|
||||||
};
|
};
|
||||||
|
|
||||||
var factory = new MqttFactory();
|
var factory = new MqttFactory();
|
||||||
|
|||||||
Reference in New Issue
Block a user