Fix web socket sub protocol negotiation.

This commit is contained in:
Christian Kratky
2020-05-16 13:01:41 +02:00
parent 43e3ad4c00
commit 48837529a2
+12 -5
View File
@@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Microsoft.Scripting.Utils; using Microsoft.Scripting.Utils;
using MQTTnet.AspNetCore;
using MQTTnet.Server.Configuration; using MQTTnet.Server.Configuration;
using MQTTnet.Server.Logging; using MQTTnet.Server.Logging;
using MQTTnet.Server.Mqtt; using MQTTnet.Server.Mqtt;
@@ -40,6 +41,9 @@ namespace MQTTnet.Server.Web
DataSharingService dataSharingService, DataSharingService dataSharingService,
MqttSettingsModel mqttSettings) MqttSettingsModel mqttSettings)
{ {
application.UseDefaultFiles();
application.UseStaticFiles();
application.UseHsts(); application.UseHsts();
application.UseRouting(); application.UseRouting();
application.UseCors(x => x application.UseCors(x => x
@@ -49,9 +53,7 @@ namespace MQTTnet.Server.Web
application.UseAuthentication(); application.UseAuthentication();
application.UseAuthorization(); application.UseAuthorization();
application.UseStaticFiles();
application.UseEndpoints(endpoints => application.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
@@ -81,7 +83,6 @@ namespace MQTTnet.Server.Web
{ {
services.AddCors(); services.AddCors();
services.AddControllers(); services.AddControllers();
services.AddMvc() services.AddMvc()
@@ -197,7 +198,13 @@ namespace MQTTnet.Server.Web
{ {
if (context.WebSockets.IsWebSocketRequest) if (context.WebSockets.IsWebSocketRequest)
{ {
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false)) string subProtocol = null;
if (context.Request.Headers.TryGetValue("Sec-WebSocket-Protocol", out var requestedSubProtocolValues))
{
subProtocol = MqttSubProtocolSelector.SelectSubProtocol(requestedSubProtocolValues);
}
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync(subProtocol).ConfigureAwait(false))
{ {
await mqttServerService.RunWebSocketConnectionAsync(webSocket, context).ConfigureAwait(false); await mqttServerService.RunWebSocketConnectionAsync(webSocket, context).ConfigureAwait(false);
} }