diff --git a/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs index 0afb64de0..a4dedff97 100644 --- a/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs @@ -1,15 +1,15 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using MQTTnet.Formatter; +using MQTTnet.Packets; +using MQTTnet.Protocol; using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; -using MQTTnet.Formatter; -using MQTTnet.Packets; -using MQTTnet.Protocol; namespace MQTTnet; @@ -138,20 +138,35 @@ public MqttClientOptionsBuilder WithConnectionUri(Uri uri) { case "tcp": case "mqtt": - WithTcpServer(uri.Host, port); + WithTcpServer(uri.Host, port) + .WithAddressFamily(AddressFamily.Unspecified) + .WithProtocolType(ProtocolType.Tcp) + .WithTlsOptions(o => o.UseTls(false)); break; case "mqtts": WithTcpServer(uri.Host, port) - .WithTlsOptions( - o => - { - }); + .WithAddressFamily(AddressFamily.Unspecified) + .WithProtocolType(ProtocolType.Tcp) + .WithTlsOptions(o => o.UseTls(true)); break; case "ws": + WithWebSocketServer(o => o.WithUri(uri.ToString())) + .WithTlsOptions(o => o.UseTls(false)); + break; + case "wss": - WithWebSocketServer(o => o.WithUri(uri.ToString())); + WithWebSocketServer(o => o.WithUri(uri.ToString())) + .WithTlsOptions(o => o.UseTls(true)); + break; + + // unix:///path/to/socket + case "unix": + WithEndPoint(new UnixDomainSocketEndPoint(uri.AbsolutePath)) + .WithAddressFamily(AddressFamily.Unix) + .WithProtocolType(ProtocolType.Unspecified) + .WithTlsOptions(o => o.UseTls(false)); break; default: diff --git a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs index 59ec023cc..6d334e809 100644 --- a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs +++ b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs @@ -66,6 +66,12 @@ public MqttClientWebSocketOptionsBuilder WithSubProtocols(ICollection su public MqttClientWebSocketOptionsBuilder WithUri(string uri) { + var webSocketUri = new Uri(uri, UriKind.Absolute); + if (webSocketUri.Scheme != Uri.UriSchemeWs && webSocketUri.Scheme != Uri.UriSchemeWss) + { + throw new ArgumentException("The scheme of the WebSocket Uri must be ws or wss."); + } + _webSocketOptions.Uri = uri; return this; }