From 0790e5479429c8159906fbc3b3aa7b6ffcecc32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicke=20Brostr=C3=B6m?= <54627464+Nickztar@users.noreply.github.com> Date: Mon, 12 Jun 2023 21:48:27 +0200 Subject: [PATCH] Websockets no longer working in Blazor WebAssembly (#1755) * Fix for websockets not applying properties unless explicitly set * Added comment indicating why properties can't be set * Update ReleaseNotes.md --------- Co-authored-by: Christian <6939810+chkr1011@users.noreply.github.com> --- .github/workflows/ReleaseNotes.md | 2 +- .../Implementations/MqttWebSocketChannel.cs | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index e48d96ff..24a926c6 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1 +1 @@ -* \ No newline at end of file +* [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar). \ No newline at end of file diff --git a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs index 6ee5b451..e83b6566 100644 --- a/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs +++ b/Source/MQTTnet/Implementations/MqttWebSocketChannel.cs @@ -221,11 +221,22 @@ namespace MQTTnet.Implementations #if !NETSTANDARD1_3 #if !WINDOWS_UWP - clientWebSocket.Options.UseDefaultCredentials = _options.UseDefaultCredentials; + // 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) + { + clientWebSocket.Options.UseDefaultCredentials = _options.UseDefaultCredentials; + } #endif - clientWebSocket.Options.KeepAliveInterval = _options.KeepAliveInterval; + if (_options.KeepAliveInterval != WebSocket.DefaultKeepAliveInterval) + { + clientWebSocket.Options.KeepAliveInterval = _options.KeepAliveInterval; + } #endif - clientWebSocket.Options.Credentials = _options.Credentials; + if (_options.Credentials != null) + { + clientWebSocket.Options.Credentials = _options.Credentials; + } var certificateValidationHandler = _options.TlsOptions?.CertificateValidationHandler; if (certificateValidationHandler != null)