Skip to content

Commit

Permalink
feat: add option to provide deflate compression options to WebSocketO…
Browse files Browse the repository at this point in the history
…ptionsBuilder
  • Loading branch information
victornor committed Dec 11, 2024
1 parent 17fdabd commit 96563b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Source/MQTTnet/Implementations/MqttWebSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ void SetupClientWebSocket(ClientWebSocket clientWebSocket)
}
}

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)
Expand Down
1 change: 1 addition & 0 deletions Source/MQTTnet/Options/MqttClientWebSocketOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
13 changes: 13 additions & 0 deletions Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.WebSockets;

namespace MQTTnet;

Expand All @@ -23,6 +24,18 @@ public MqttClientWebSocketOptionsBuilder WithCookieContainer(CookieContainer coo
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)
{
_webSocketOptions.Credentials = credentials;
Expand Down

0 comments on commit 96563b6

Please sign in to comment.