Skip to content

Commit

Permalink
Refactor options.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Jul 26, 2018
1 parent 0839fa5 commit f70c79a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ This project also listed at Open Collective (https://opencollective.com/mqttnet)

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 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.

Expand Down
33 changes: 18 additions & 15 deletions Source/MQTTnet/Client/MqttClientOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MqttClientOptionsBuilder
private MqttClientTcpOptions _tcpOptions;
private MqttClientWebSocketOptions _webSocketOptions;
private MqttClientOptionsBuilderTlsParameters _tlsParameters;
private MqttClientWebSocketProxyOptions _proxyOptions;

public MqttClientOptionsBuilder WithProtocolVersion(MqttProtocolVersion value)
{
Expand Down Expand Up @@ -76,16 +77,9 @@ public MqttClientOptionsBuilder WithTcpServer(string server, int? port = null)
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)
{
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,
Username = username,
Expand All @@ -97,7 +91,6 @@ public MqttClientOptionsBuilder WithProxy(string address, string username = null

return this;
}
#endif

public MqttClientOptionsBuilder WithWebSocketServer(string uri)
{
Expand All @@ -120,7 +113,7 @@ public MqttClientOptionsBuilder WithTls()
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(
bool allowUntrustedCertificates = false,
bool ignoreCertificateChainErrors = false,
Expand All @@ -141,13 +134,13 @@ public MqttClientOptionsBuilder WithTls(

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)
{
var tlsOptions = new MqttClientTlsOptions
Expand All @@ -172,6 +165,16 @@ public IMqttClientOptions Build()
}
}

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

_webSocketOptions.ProxyOptions = _proxyOptions;
}

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

return _options;
Expand Down
2 changes: 1 addition & 1 deletion Source/MQTTnet/Client/MqttClientWebSocketProxyOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MqttClientWebSocketProxyOptions

public string Domain { get; set; }

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

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

0 comments on commit f70c79a

Please sign in to comment.