Browse Source

Refactor options.

release/3.x.x
Christian Kratky 6 years ago
parent
commit
f70c79aaf9
3 changed files with 21 additions and 17 deletions
  1. +2
    -1
      README.md
  2. +18
    -15
      Source/MQTTnet/Client/MqttClientOptionsBuilder.cs
  3. +1
    -1
      Source/MQTTnet/Client/MqttClientWebSocketProxyOptions.cs

+ 2
- 1
README.md View File

@@ -83,9 +83,10 @@ This project also listed at Open Collective (https://opencollective.com/mqttnet)


This library is used in the following projects: This library is used in the following projects:


* HA4IoT (Open Source Home Automation system for .NET, <https://github.com/chkr1011/HA4IoT>)
* MQTT Client Rx (Wrapper for Reactive Extensions, <https://github.com/1iveowl/MQTTClient.rx>) * MQTT Client Rx (Wrapper for Reactive Extensions, <https://github.com/1iveowl/MQTTClient.rx>)
* MQTT Tester (MQTT client test app for [Android](https://play.google.com/store/apps/details?id=com.liveowl.mqtttester) and [iOS](https://itunes.apple.com/us/app/mqtt-tester/id1278621826?mt=8)) * MQTT Tester (MQTT client test app for [Android](https://play.google.com/store/apps/details?id=com.liveowl.mqtttester) and [iOS](https://itunes.apple.com/us/app/mqtt-tester/id1278621826?mt=8))
* HA4IoT (Open Source Home Automation system for .NET, <https://github.com/chkr1011/HA4IoT>)
* Wirehome.Core (Open Source Home Automation system for .NET Core, <https://github.com/chkr1011/Wirehome.Core>)


If you use this library and want to see your project here please let me know. If you use this library and want to see your project here please let me know.




+ 18
- 15
Source/MQTTnet/Client/MqttClientOptionsBuilder.cs View File

@@ -11,6 +11,7 @@ namespace MQTTnet.Client
private MqttClientTcpOptions _tcpOptions; private MqttClientTcpOptions _tcpOptions;
private MqttClientWebSocketOptions _webSocketOptions; private MqttClientWebSocketOptions _webSocketOptions;
private MqttClientOptionsBuilderTlsParameters _tlsParameters; private MqttClientOptionsBuilderTlsParameters _tlsParameters;
private MqttClientWebSocketProxyOptions _proxyOptions;


public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion value) public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion value)
{ {
@@ -76,16 +77,9 @@ namespace MQTTnet.Client
return this; return this;
} }


#if NET452 || NET461

public MqttClientOptionsBuilder WithProxy(string address, string username = null, string password = null, string domain = null, bool bypassOnLocal = false, string[] bypassList = null) public MqttClientOptionsBuilder WithProxy(string address, string username = null, string password = null, string domain = null, bool bypassOnLocal = false, string[] bypassList = null)
{ {
if (_webSocketOptions == null)
{
throw new InvalidOperationException("A WebSocket channel must be set if MqttClientWebSocketProxy is configured.");
}

_webSocketOptions.ProxyOptions = new MqttClientWebSocketProxyOptions
_proxyOptions = new MqttClientWebSocketProxyOptions
{ {
Address = address, Address = address,
Username = username, Username = username,
@@ -97,7 +91,6 @@ namespace MQTTnet.Client


return this; return this;
} }
#endif


public MqttClientOptionsBuilder WithWebSocketServer(string uri) public MqttClientOptionsBuilder WithWebSocketServer(string uri)
{ {
@@ -120,7 +113,7 @@ namespace MQTTnet.Client
return WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = true }); return WithTls(new MqttClientOptionsBuilderTlsParameters { UseTls = true });
} }


[Obsolete("Use method _WithTlps_ which accepts the _MqttClientOptionsBuilderTlsParameters_.")]
[Obsolete("Use method _WithTls_ which accepts the _MqttClientOptionsBuilderTlsParameters_.")]
public MqttClientOptionsBuilder WithTls( public MqttClientOptionsBuilder WithTls(
bool allowUntrustedCertificates = false, bool allowUntrustedCertificates = false,
bool ignoreCertificateChainErrors = false, bool ignoreCertificateChainErrors = false,
@@ -141,13 +134,13 @@ namespace MQTTnet.Client


public IMqttClientOptions Build() public IMqttClientOptions Build()
{ {
if (_tlsParameters != null)
if (_tcpOptions == null && _webSocketOptions == null)
{ {
if (_tcpOptions == null && _webSocketOptions == null)
{
throw new InvalidOperationException("A channel (TCP or WebSocket) must be set if TLS is configured.");
}
throw new InvalidOperationException("A channel must be set.");
}


if (_tlsParameters != null)
{
if (_tlsParameters?.UseTls == true) if (_tlsParameters?.UseTls == true)
{ {
var tlsOptions = new MqttClientTlsOptions var tlsOptions = new MqttClientTlsOptions
@@ -172,6 +165,16 @@ namespace MQTTnet.Client
} }
} }


if (_proxyOptions != null)
{
if (_webSocketOptions == null)
{
throw new InvalidOperationException("Proxies are only supported for WebSocket connections.");
}

_webSocketOptions.ProxyOptions = _proxyOptions;
}

_options.ChannelOptions = (IMqttClientChannelOptions)_tcpOptions ?? _webSocketOptions; _options.ChannelOptions = (IMqttClientChannelOptions)_tcpOptions ?? _webSocketOptions;


return _options; return _options;


+ 1
- 1
Source/MQTTnet/Client/MqttClientWebSocketProxyOptions.cs View File

@@ -10,7 +10,7 @@


public string Domain { get; set; } public string Domain { get; set; }


public bool BypassOnLocal { get; set; } = true;
public bool BypassOnLocal { get; set; }


public string[] BypassList { get; set; } public string[] BypassList { get; set; }
} }

Loading…
Cancel
Save