mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Merge branch 'master' into feature/low_level_client
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using MQTTnet.Client.Options;
|
||||
|
||||
namespace MQTTnet.Extensions
|
||||
{
|
||||
public static class MqttClientOptionsBuilderExtension
|
||||
{
|
||||
public static MqttClientOptionsBuilder WithConnectionUri(this MqttClientOptionsBuilder builder, Uri uri)
|
||||
{
|
||||
var port = uri.IsDefaultPort ? null : (int?) uri.Port;
|
||||
switch (uri.Scheme.ToLower())
|
||||
{
|
||||
case "tcp":
|
||||
case "mqtt":
|
||||
builder.WithTcpServer(uri.Host, port);
|
||||
break;
|
||||
|
||||
case "mqtts":
|
||||
builder.WithTcpServer(uri.Host, port).WithTls();
|
||||
break;
|
||||
|
||||
case "ws":
|
||||
case "wss":
|
||||
builder.WithWebSocketServer(uri.ToString());
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentException("Unexpected scheme in uri.");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(uri.UserInfo))
|
||||
{
|
||||
var userInfo = uri.UserInfo.Split(':');
|
||||
var username = userInfo[0];
|
||||
var password = userInfo.Length > 1 ? userInfo[1] : "";
|
||||
builder.WithCredentials(username, password);
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static MqttClientOptionsBuilder WithConnectionUri(this MqttClientOptionsBuilder builder, string uri)
|
||||
{
|
||||
return WithConnectionUri(builder, new Uri(uri, UriKind.Absolute));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace MQTTnet.Extensions
|
||||
{
|
||||
public static class UserPropertyExtension
|
||||
{
|
||||
public static string GetUserProperty(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
|
||||
{
|
||||
if (message == null) throw new ArgumentNullException(nameof(message));
|
||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||
|
||||
return message.UserProperties?.SingleOrDefault(up => up.Name.Equals(propertyName, comparisonType))?.Value;
|
||||
}
|
||||
|
||||
public static T GetUserProperty<T>(this MqttApplicationMessage message, string propertyName, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
|
||||
{
|
||||
var value = GetUserProperty(message, propertyName, comparisonType);
|
||||
|
||||
var typeDescriptor = TypeDescriptor.GetConverter(typeof(T));
|
||||
try
|
||||
{
|
||||
return (T) typeDescriptor.ConvertFromString(value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException($"Cannot convert value({value ?? "null"}) of UserProperty({propertyName}) to {typeof(T).FullName}.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,6 @@ namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
if (applicationMessage == null) throw new ArgumentNullException(nameof(applicationMessage));
|
||||
|
||||
if (applicationMessage.UserProperties?.Any() == true)
|
||||
{
|
||||
throw new MqttProtocolViolationException("User properties are not supported in MQTT version 3.");
|
||||
}
|
||||
|
||||
return new MqttPublishPacket
|
||||
{
|
||||
Topic = applicationMessage.Topic,
|
||||
@@ -171,11 +166,6 @@ namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
if (options.UserProperties?.Any() == true)
|
||||
{
|
||||
throw new MqttProtocolViolationException("User properties are not supported in MQTT version 3.");
|
||||
}
|
||||
|
||||
var subscribePacket = new MqttSubscribePacket();
|
||||
subscribePacket.TopicFilters.AddRange(options.TopicFilters);
|
||||
|
||||
@@ -186,11 +176,6 @@ namespace MQTTnet.Formatter.V3
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
|
||||
if (options.UserProperties?.Any() == true)
|
||||
{
|
||||
throw new MqttProtocolViolationException("User properties are not supported in MQTT version 3.");
|
||||
}
|
||||
|
||||
var unsubscribePacket = new MqttUnsubscribePacket();
|
||||
unsubscribePacket.TopicFilters.AddRange(options.TopicFilters);
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
|
||||
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
|
||||
<PackageReference Include="System.Net.Security" Version="4.3.2" />
|
||||
<PackageReference Include="System.Net.WebSockets" Version="4.3.0" />
|
||||
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.2" />
|
||||
|
||||
Reference in New Issue
Block a user