diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index e6ff10f..1ed1188 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -16,6 +16,7 @@ * [Client] Added a first version of a managed client which will manage the connection, subscription etc. automatically (Thanks to @JTrotta) * [Client] The session state response from the server is now returned in the _ConnectAsync_ method and also part of the _Connected_ event args * [Client] Added a _TopicFilterBuilder_ using a fluent API (Namespace Changes!) +* [Client] Added several new options for the WebSocket channel (Thanks to @ChristianRiedl) * [Server] Added support for WebSockets via ASP.NET Core 2.0 (Thanks to @ChristianRiedl) * [Server] Added support for a custom application message interceptor diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs index 0433b80..081c74a 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttWebSocketChannel.cs @@ -3,6 +3,7 @@ using MQTTnet.Core.Client; using System; using System.IO; using System.Net.WebSockets; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; @@ -32,6 +33,37 @@ namespace MQTTnet.Implementations _webSocket = new ClientWebSocket(); _webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod; + + if (_options.RequestHeaders != null) + { + foreach (var requestHeader in _options.RequestHeaders) + { + _webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value); + } + } + + if (_options.SubProtocols != null) + { + foreach (var subProtocol in _options.SubProtocols) + { + _webSocket.Options.AddSubProtocol(subProtocol); + } + } + + if (_options.CookieContainer != null) + { + _webSocket.Options.Cookies = _options.CookieContainer; + } + + if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null) + { + _webSocket.Options.ClientCertificates = new X509CertificateCollection(); + foreach (var certificate in _options.TlsOptions.Certificates) + { + _webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate)); + } + } + await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None); RawReceiveStream = new WebSocketStream(_webSocket); } diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs index 471cdbf..4843316 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttWebSocketChannel.cs @@ -3,6 +3,7 @@ using MQTTnet.Core.Client; using System; using System.IO; using System.Net.WebSockets; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; @@ -32,6 +33,37 @@ namespace MQTTnet.Implementations _webSocket = new ClientWebSocket(); _webSocket.Options.KeepAliveInterval = _options.KeepAlivePeriod; + + if (_options.RequestHeaders != null) + { + foreach (var requestHeader in _options.RequestHeaders) + { + _webSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value); + } + } + + if (_options.SubProtocols != null) + { + foreach (var subProtocol in _options.SubProtocols) + { + _webSocket.Options.AddSubProtocol(subProtocol); + } + } + + if (_options.CookieContainer != null) + { + _webSocket.Options.Cookies = _options.CookieContainer; + } + + if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null) + { + _webSocket.Options.ClientCertificates = new X509CertificateCollection(); + foreach (var certificate in _options.TlsOptions.Certificates) + { + _webSocket.Options.ClientCertificates.Add(new X509Certificate(certificate)); + } + } + await _webSocket.ConnectAsync(new Uri(uri), CancellationToken.None).ConfigureAwait(false); RawReceiveStream = new WebSocketStream(_webSocket); diff --git a/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs b/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs index b2b5777..522fb9b 100644 --- a/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs +++ b/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs @@ -1,7 +1,16 @@ -namespace MQTTnet.Core.Client +using System.Collections.Generic; +using System.Net; + +namespace MQTTnet.Core.Client { public class MqttClientWebSocketOptions : BaseMqttClientOptions { public string Uri { get; set; } + + public IDictionary RequestHeaders { get; set; } + + public ICollection SubProtocols { get; set; } + + public CookieContainer CookieContainer { get; set; } } }