From 9f81a2bb710547d1388eed975067b8e5e4bb29ef Mon Sep 17 00:00:00 2001 From: Victornor <1-1@hotmail.dk> Date: Mon, 23 Dec 2024 11:21:46 +0100 Subject: [PATCH] Add deflate compressions options to MqttClientWebSocketOptionsBuilder (#2127) * fix: unused tryPrivate parameter in WithTryPrivate * Fix wrong usage of try private * feat: add option to provide deflate compression options to WebSocketOptionsBuilder --------- Co-authored-by: christian <6939810+chkr1011@users.noreply.github.com> --- .../MQTTnet/Implementations/MqttWebSocketChannel.cs | 3 +++ .../MQTTnet/Options/MqttClientWebSocketOptions.cs | 1 + .../Options/MqttClientWebSocketOptionsBuilder.cs | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs index 8c7e5911..c8346011 100644 --- a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs +++ b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs @@ -200,6 +200,9 @@ namespace MQTTnet.Implementations } } + if (_options.DangerousDeflateOptions != null) + clientWebSocket.Options.DangerousDeflateOptions = _options.DangerousDeflateOptions; + // Only set the value if it is actually true. This property is not supported on all platforms // and will throw a _PlatformNotSupported_ (i.e. WASM) exception when being used regardless of the actual value. if (_options.UseDefaultCredentials) diff --git a/Source/MQTTnet/Options/MqttClientWebSocketOptions.cs b/Source/MQTTnet/Options/MqttClientWebSocketOptions.cs index a972bd91..1e2d9811 100644 --- a/Source/MQTTnet/Options/MqttClientWebSocketOptions.cs +++ b/Source/MQTTnet/Options/MqttClientWebSocketOptions.cs @@ -12,6 +12,7 @@ namespace MQTTnet; public sealed class MqttClientWebSocketOptions : IMqttClientChannelOptions { public CookieContainer CookieContainer { get; set; } + public WebSocketDeflateOptions DangerousDeflateOptions { get; set; } public ICredentials Credentials { get; set; } diff --git a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs index 6d334e80..fffc77ef 100644 --- a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs +++ b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.WebSockets; namespace MQTTnet; @@ -23,6 +24,18 @@ public sealed class MqttClientWebSocketOptionsBuilder return this; } + /// + /// Allows the client to negotiate deflate compression on every message, by using the permessage-deflate WebSocket extension. + /// This adds the Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits header. + /// + /// + /// + public MqttClientWebSocketOptionsBuilder WithDangerousDeflateOptions(WebSocketDeflateOptions dangerousDeflateOptions) + { + _webSocketOptions.DangerousDeflateOptions = dangerousDeflateOptions; + return this; + } + public MqttClientWebSocketOptionsBuilder WithCookieContainer(ICredentials credentials) { _webSocketOptions.Credentials = credentials;