static void ConfigureMqttWebSocketEndpoint(IApplicationBuilder app, MqttService mqttService) { app.Use(async(context, next) => { if (context.Request.Path == "/mqtt") { if (context.WebSockets.IsWebSocketRequest) { 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 mqttService.RunWebSocketConnectionAsync(webSocket, context).ConfigureAwait(false); } } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; } } else { await next().ConfigureAwait(false); } }); }