public async Task RunWebSocketConnectionAsync(WebSocket webSocket, HttpContext httpContext) { if (webSocket == null) { throw new ArgumentNullException(nameof(webSocket)); } var endpoint = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}"; var clientCertificate = await httpContext.Connection.GetClientCertificateAsync().ConfigureAwait(false); var isSecureConnection = clientCertificate != null; clientCertificate?.Dispose(); var clientHandler = ClientHandler; if (clientHandler != null) { var writer = new SpanBasedMqttPacketWriter(); var formatter = new MqttPacketFormatterAdapter(writer); var channel = new MqttWebSocketChannel(webSocket, endpoint, isSecureConnection); using (var channelAdapter = new MqttChannelAdapter(channel, formatter, _logger.CreateChildLogger(nameof(MqttWebSocketServerAdapter)))) { await clientHandler(channelAdapter).ConfigureAwait(false); } } }
public async Task RunWebSocketConnectionAsync(WebSocket webSocket, HttpContext httpContext) { if (webSocket == null) { throw new ArgumentNullException(nameof(webSocket)); } var endpoint = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}"; var clientCertificate = await httpContext.Connection.GetClientCertificateAsync().ConfigureAwait(false); try { var isSecureConnection = clientCertificate != null; var clientHandler = ClientHandler; if (clientHandler != null) { var formatter = new MqttPacketFormatterAdapter(new MqttBufferWriter(4096, 65535)); var channel = new MqttWebSocketChannel(webSocket, endpoint, isSecureConnection, clientCertificate); using (var channelAdapter = new MqttChannelAdapter(channel, formatter, null, _logger)) { await clientHandler(channelAdapter).ConfigureAwait(false); } } } finally { clientCertificate?.Dispose(); } }