mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-21 04:03:05 +08:00
Add new samples.
This commit is contained in:
@@ -40,6 +40,53 @@ public static class Client_Publish_Samples
|
|||||||
|
|
||||||
await mqttClient.PublishAsync(applicationMessage, CancellationToken.None);
|
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 MqttFactory();
|
||||||
|
|
||||||
|
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.");
|
Console.WriteLine("MQTT application message is published.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user