fixed websocket subprotocol negotiation

This commit is contained in:
JanEggers
2017-11-12 16:46:32 +01:00
parent 48c47c6fcf
commit 8672fb2792
2 changed files with 19 additions and 5 deletions
@@ -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);
} }
+4 -4
View File
@@ -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();