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>
This commit is contained in:
Victornor
2024-12-23 11:21:46 +01:00
committed by GitHub
co-authored by christian
parent e2f8d0f0ac
commit 9f81a2bb71
3 changed files with 17 additions and 0 deletions
@@ -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 // 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. // and will throw a _PlatformNotSupported_ (i.e. WASM) exception when being used regardless of the actual value.
if (_options.UseDefaultCredentials) if (_options.UseDefaultCredentials)
@@ -12,6 +12,7 @@ namespace MQTTnet;
public sealed class MqttClientWebSocketOptions : IMqttClientChannelOptions public sealed class MqttClientWebSocketOptions : IMqttClientChannelOptions
{ {
public CookieContainer CookieContainer { get; set; } public CookieContainer CookieContainer { get; set; }
public WebSocketDeflateOptions DangerousDeflateOptions { get; set; }
public ICredentials Credentials { get; set; } public ICredentials Credentials { get; set; }
@@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.WebSockets;
namespace MQTTnet; namespace MQTTnet;
@@ -23,6 +24,18 @@ public sealed class MqttClientWebSocketOptionsBuilder
return this; return this;
} }
/// <summary>
/// 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.
/// </summary>
/// <param name="dangerousDeflateOptions"></param>
/// <returns></returns>
public MqttClientWebSocketOptionsBuilder WithDangerousDeflateOptions(WebSocketDeflateOptions dangerousDeflateOptions)
{
_webSocketOptions.DangerousDeflateOptions = dangerousDeflateOptions;
return this;
}
public MqttClientWebSocketOptionsBuilder WithCookieContainer(ICredentials credentials) public MqttClientWebSocketOptionsBuilder WithCookieContainer(ICredentials credentials)
{ {
_webSocketOptions.Credentials = credentials; _webSocketOptions.Credentials = credentials;