mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-17 01:33:03 +08:00
72 lines
2.9 KiB
C#
72 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using MQTTnet.Protocol;
|
|
|
|
namespace MQTTnet
|
|
{
|
|
public static class ApplicationMessagePublisherExtensions
|
|
{
|
|
public static async Task PublishAsync(this IApplicationMessagePublisher publisher, IEnumerable<MqttApplicationMessage> applicationMessages)
|
|
{
|
|
if (publisher == null) throw new ArgumentNullException(nameof(publisher));
|
|
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
|
|
|
|
foreach (var applicationMessage in applicationMessages)
|
|
{
|
|
await publisher.PublishAsync(applicationMessage);
|
|
}
|
|
}
|
|
|
|
public static async Task PublishAsync(this IApplicationMessagePublisher publisher, params MqttApplicationMessage[] applicationMessages)
|
|
{
|
|
if (publisher == null) throw new ArgumentNullException(nameof(publisher));
|
|
if (applicationMessages == null) throw new ArgumentNullException(nameof(applicationMessages));
|
|
|
|
foreach (var applicationMessage in applicationMessages)
|
|
{
|
|
await publisher.PublishAsync(applicationMessage);
|
|
}
|
|
}
|
|
|
|
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic)
|
|
{
|
|
if (publisher == null) throw new ArgumentNullException(nameof(publisher));
|
|
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
|
|
|
return publisher.PublishAsync(builder => builder
|
|
.WithTopic(topic)
|
|
);
|
|
}
|
|
|
|
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload)
|
|
{
|
|
if (publisher == null) throw new ArgumentNullException(nameof(publisher));
|
|
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
|
|
|
return publisher.PublishAsync(builder => builder
|
|
.WithTopic(topic)
|
|
.WithPayload(payload)
|
|
);
|
|
}
|
|
|
|
public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
|
|
{
|
|
if (publisher == null) throw new ArgumentNullException(nameof(publisher));
|
|
if (topic == null) throw new ArgumentNullException(nameof(topic));
|
|
|
|
return publisher.PublishAsync(builder => builder
|
|
.WithTopic(topic)
|
|
.WithPayload(payload)
|
|
.WithQualityOfServiceLevel(qualityOfServiceLevel)
|
|
);
|
|
}
|
|
|
|
public static Task PublishAsync(this IApplicationMessagePublisher publisher, Func<MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
|
|
{
|
|
var message = builder(new MqttApplicationMessageBuilder()).Build();
|
|
return publisher.PublishAsync(message);
|
|
}
|
|
}
|
|
}
|