瀏覽代碼

Add subscribe options builder.

release/3.x.x
Christian Kratky 5 年之前
父節點
當前提交
e068286f15
共有 2 個文件被更改,包括 88 次插入1 次删除
  1. +1
    -1
      Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs
  2. +87
    -0
      Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs

+ 1
- 1
Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptions.cs 查看文件

@@ -9,6 +9,6 @@ namespace MQTTnet.Client.Subscribing

public uint? SubscriptionIdentifier { get; set; }

public List<MqttUserProperty> UserProperties { get; set; } = new List<MqttUserProperty>();
public List<MqttUserProperty> UserProperties { get; set; }
}
}

+ 87
- 0
Source/MQTTnet/Client/Subscribing/MqttClientSubscribeOptionsBuilder.cs 查看文件

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using MQTTnet.Packets;
using MQTTnet.Protocol;

namespace MQTTnet.Client.Subscribing
{
public class MqttClientSubscribeOptionsBuilder
{
private readonly MqttClientSubscribeOptions _subscribeOptions = new MqttClientSubscribeOptions();

public MqttClientSubscribeOptionsBuilder WithUserProperty(string name, string value)
{
if (name == null) throw new ArgumentNullException(nameof(name));
if (value == null) throw new ArgumentNullException(nameof(value));

if (_subscribeOptions.UserProperties == null)
{
_subscribeOptions.UserProperties = new List<MqttUserProperty>();
}

_subscribeOptions.UserProperties.Add(new MqttUserProperty(name, value));

return this;
}

public MqttClientSubscribeOptionsBuilder WithSubscriptionIdentifier(uint? subscriptionIdentifier)
{
_subscribeOptions.SubscriptionIdentifier = subscriptionIdentifier;

return this;
}

public MqttClientSubscribeOptionsBuilder WithTopicFilter(
string topic,
MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
bool? noLocal = null,
bool? retainAsPublished = null,
MqttRetainHandling? retainHandling = null)
{
return WithTopicFilter(new TopicFilter
{
Topic = topic,
QualityOfServiceLevel = qualityOfServiceLevel,
NoLocal = noLocal,
RetainAsPublished = retainAsPublished,
RetainHandling = retainHandling
});
}

public MqttClientSubscribeOptionsBuilder WithTopicFilter(Action<TopicFilterBuilder> topicFilterBuilder)
{
if (topicFilterBuilder == null) throw new ArgumentNullException(nameof(topicFilterBuilder));

var internalTopicFilterBuilder = new TopicFilterBuilder();
topicFilterBuilder(internalTopicFilterBuilder);

return WithTopicFilter(internalTopicFilterBuilder);
}

public MqttClientSubscribeOptionsBuilder WithTopicFilter(TopicFilterBuilder topicFilterBuilder)
{
if (topicFilterBuilder == null) throw new ArgumentNullException(nameof(topicFilterBuilder));

return WithTopicFilter(topicFilterBuilder.Build());
}

public MqttClientSubscribeOptionsBuilder WithTopicFilter(TopicFilter topicFilter)
{
if (topicFilter == null) throw new ArgumentNullException(nameof(topicFilter));

if (_subscribeOptions.TopicFilters == null)
{
_subscribeOptions.TopicFilters = new List<TopicFilter>();
}

_subscribeOptions.TopicFilters.Add(topicFilter);

return this;
}

public MqttClientSubscribeOptions Build()
{
return _subscribeOptions;
}
}
}

Loading…
取消
儲存