mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
* Add support for binary user properties in MQTT 5.0 * Optimize byte array reversal in SubscriptionTopicHashTests * Use InvariantCulture for topic string conversion in Load_Tests * switched to `Microsoft.Testing.Platform` * Refactor CI workflow to separate signed and unsigned package builds * Add condition to sign job to prevent execution on external pull requests * Remove fallback to value as string in MqttUserProperty * Refactor MqttUserProperty handling to use byte arrays for improved performance and add deprecation warnings for legacy methods
107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
// 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.
|
|
|
|
// ReSharper disable UnusedType.Global
|
|
// ReSharper disable UnusedMember.Global
|
|
// ReSharper disable InconsistentNaming
|
|
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace MQTTnet.Samples.Client;
|
|
|
|
public static class Client_Publish_Samples
|
|
{
|
|
public static async Task Publish_Application_Message()
|
|
{
|
|
/*
|
|
* This sample pushes a simple application message including a topic and a payload.
|
|
*
|
|
* Always use builders where they exist. Builders (in this project) are designed to be
|
|
* backward compatible. Creating an _MqttApplicationMessage_ via its constructor is also
|
|
* supported but the class might change often in future releases where the builder does not
|
|
* or at least provides backward compatibility where possible.
|
|
*/
|
|
|
|
var mqttFactory = new MqttClientFactory();
|
|
|
|
using var mqttClient = mqttFactory.CreateMqttClient();
|
|
var mqttClientOptions = new MqttClientOptionsBuilder()
|
|
.WithTcpServer("broker.hivemq.com")
|
|
.Build();
|
|
|
|
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
|
|
|
var applicationMessage = new MqttApplicationMessageBuilder()
|
|
.WithTopic("samples/temperature/living_room")
|
|
.WithPayload("19.5")
|
|
.Build();
|
|
|
|
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
|
|
|
|
await mqttClient.DisconnectAsync();
|
|
|
|
Console.WriteLine("MQTT application message is published.");
|
|
}
|
|
|
|
public static async Task Publish_Multiple_Application_Messages()
|
|
{
|
|
/*
|
|
* This sample pushes multiple simple application message including a topic and a payload.
|
|
*
|
|
* See sample _Publish_Application_Message_ for more details.
|
|
*/
|
|
|
|
var mqttFactory = new MqttClientFactory();
|
|
|
|
using var mqttClient = mqttFactory.CreateMqttClient();
|
|
var mqttClientOptions = new MqttClientOptionsBuilder()
|
|
.WithTcpServer("broker.hivemq.com")
|
|
.Build();
|
|
|
|
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
|
|
|
|
var applicationMessage = new MqttApplicationMessageBuilder()
|
|
.WithTopic("samples/temperature/living_room")
|
|
.WithPayload("19.5")
|
|
.Build();
|
|
|
|
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
|
|
|
|
applicationMessage = new MqttApplicationMessageBuilder()
|
|
.WithTopic("samples/temperature/living_room")
|
|
.WithPayload("20.0")
|
|
.Build();
|
|
|
|
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
|
|
|
|
applicationMessage = new MqttApplicationMessageBuilder()
|
|
.WithTopic("samples/temperature/living_room")
|
|
.WithPayload("21.0")
|
|
.Build();
|
|
|
|
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
|
|
|
|
await mqttClient.DisconnectAsync();
|
|
|
|
Console.WriteLine("MQTT application message is published.");
|
|
}
|
|
|
|
public static MqttApplicationMessage Create_Message_With_Binary_User_Property()
|
|
{
|
|
/*
|
|
* MQTT v5 user properties are encoded as UTF-8 strings. When the UTF-8 payload is already available
|
|
* as a byte buffer, the builder APIs can avoid creating intermediate strings.
|
|
*/
|
|
|
|
var encodedValue = Encoding.UTF8.GetBytes("sensor-01");
|
|
|
|
return new MqttApplicationMessageBuilder()
|
|
.WithTopic("samples/metadata/binary")
|
|
.WithUserProperty("client-id", encodedValue.AsMemory())
|
|
.WithUserProperty("checksum", new ArraySegment<byte>(encodedValue))
|
|
.WithPayload("metadata")
|
|
.Build();
|
|
}
|
|
} |