From 0c13e7a5044394568a921de47fd86ae10fcb2b5d Mon Sep 17 00:00:00 2001 From: Charles Quesnel Date: Thu, 13 Dec 2018 12:56:47 -0500 Subject: [PATCH 1/4] Add overload to allow passing custom request headers and cookies when connecting with WS --- .../Client/MqttClientOptionsBuilder.cs | 19 +++++++++++++++++++ ...ClientOptionsBuilderWebSocketParameters.cs | 14 ++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs index 85e2b4d..e4754ad 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Net; using MQTTnet.Serializer; namespace MQTTnet.Client @@ -102,6 +104,23 @@ namespace MQTTnet.Client return this; } + public MqttClientOptionsBuilder WithWebSocketServer(string uri, MqttClientOptionsBuilderWebSocketParameters parameters) + { + if(parameters == null) + { + throw new ArgumentNullException(nameof(parameters)); + } + + _webSocketOptions = new MqttClientWebSocketOptions + { + Uri = uri, + RequestHeaders = parameters.RequestHeaders, + CookieContainer = parameters.CookierContainer + }; + + return this; + } + public MqttClientOptionsBuilder WithTls(MqttClientOptionsBuilderTlsParameters parameters) { _tlsParameters = parameters ?? throw new ArgumentNullException(nameof(parameters)); diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs new file mode 100644 index 0000000..c58d777 --- /dev/null +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; + +namespace MQTTnet.Client +{ + public class MqttClientOptionsBuilderWebSocketParameters + { + public IDictionary RequestHeaders { get; set; } + + public CookieContainer CookierContainer { get; set; } + } +} From a94444098a11359919f69636d3a86d089ac7b3be Mon Sep 17 00:00:00 2001 From: Charles Quesnel Date: Thu, 13 Dec 2018 13:01:34 -0500 Subject: [PATCH 2/4] Remove unused usings --- Source/MQTTnet/Client/MqttClientOptionsBuilder.cs | 2 -- .../Client/MqttClientOptionsBuilderWebSocketParameters.cs | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs index e4754ad..07e9819 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Net; using MQTTnet.Serializer; namespace MQTTnet.Client diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs index c58d777..7ecb230 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net; -using System.Text; namespace MQTTnet.Client { From 40326df9a9dfe06634361f9b6e565c3acf7dcf50 Mon Sep 17 00:00:00 2001 From: Charles Quesnel Date: Thu, 13 Dec 2018 14:08:53 -0500 Subject: [PATCH 3/4] Fix typo in 'Cookie' --- Source/MQTTnet/Client/MqttClientOptionsBuilder.cs | 2 +- .../Client/MqttClientOptionsBuilderWebSocketParameters.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs index 07e9819..e612981 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs @@ -113,7 +113,7 @@ namespace MQTTnet.Client { Uri = uri, RequestHeaders = parameters.RequestHeaders, - CookieContainer = parameters.CookierContainer + CookieContainer = parameters.CookieContainer }; return this; diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs index 7ecb230..54e048b 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilderWebSocketParameters.cs @@ -7,6 +7,6 @@ namespace MQTTnet.Client { public IDictionary RequestHeaders { get; set; } - public CookieContainer CookierContainer { get; set; } + public CookieContainer CookieContainer { get; set; } } } From dfda9162839842eb56e80f0c528d045e32a35b91 Mon Sep 17 00:00:00 2001 From: Charles Quesnel Date: Thu, 13 Dec 2018 14:54:31 -0500 Subject: [PATCH 4/4] Use default parameter instead of overload --- .../Client/MqttClientOptionsBuilder.cs | 21 +++---------------- 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs index e612981..aaf4f14 100644 --- a/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Client/MqttClientOptionsBuilder.cs @@ -92,28 +92,13 @@ namespace MQTTnet.Client return this; } - public MqttClientOptionsBuilder WithWebSocketServer(string uri) + public MqttClientOptionsBuilder WithWebSocketServer(string uri, MqttClientOptionsBuilderWebSocketParameters parameters = null) { - _webSocketOptions = new MqttClientWebSocketOptions - { - Uri = uri - }; - - return this; - } - - public MqttClientOptionsBuilder WithWebSocketServer(string uri, MqttClientOptionsBuilderWebSocketParameters parameters) - { - if(parameters == null) - { - throw new ArgumentNullException(nameof(parameters)); - } - _webSocketOptions = new MqttClientWebSocketOptions { Uri = uri, - RequestHeaders = parameters.RequestHeaders, - CookieContainer = parameters.CookieContainer + RequestHeaders = parameters?.RequestHeaders, + CookieContainer = parameters?.CookieContainer }; return this;