mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
fixed websocket subprotocol negotiation
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MQTTnet.Core.Server;
|
||||
using System.Linq;
|
||||
|
||||
namespace MQTTnet.AspNetCore
|
||||
{
|
||||
@@ -14,8 +15,21 @@ namespace MQTTnet.AspNetCore
|
||||
{
|
||||
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>();
|
||||
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync("mqtt"))
|
||||
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subprotocol))
|
||||
{
|
||||
await adapter.AcceptWebSocketAsync(webSocket);
|
||||
}
|
||||
|
||||
@@ -21,10 +21,10 @@ namespace MQTTnet.TestApp.NetCore
|
||||
{
|
||||
Server = "localhost"
|
||||
},
|
||||
////ChannelOptions = new MqttClientWebSocketOptions
|
||||
////{
|
||||
//// Uri = "localhost"
|
||||
////}
|
||||
//ChannelOptions = new MqttClientWebSocketOptions
|
||||
//{
|
||||
// Uri = "ws://localhost:59690/mqtt"
|
||||
//}
|
||||
};
|
||||
|
||||
var factory = new MqttFactory();
|
||||
|
||||
Reference in New Issue
Block a user