|
- // 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.
-
- using MQTTnet.Client;
- using MQTTnet.Samples.Helpers;
-
- namespace MQTTnet.Samples.Client;
-
- public static class Client_Subscribe_Samples
- {
- public static async Task Handle_Received_Application_Message()
- {
- /*
- * This sample subscribes to a topic and processes the received message.
- */
-
- var mqttFactory = new MqttFactory();
-
- using (var mqttClient = mqttFactory.CreateMqttClient())
- {
- var mqttClientOptions = new MqttClientOptionsBuilder()
- .WithTcpServer("broker.hivemq.com")
- .Build();
-
- // Setup message handling before connecting so that queued messages
- // are also handled properly. When there is no event handler attached all
- // received messages get lost.
- mqttClient.ApplicationMessageReceivedAsync += e =>
- {
- Console.WriteLine("Received application message.");
- e.DumpToConsole();
-
- return Task.CompletedTask;
- };
-
- await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
-
- var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
- .WithTopicFilter(f => { f.WithTopic("mqttnet/samples/topic/2"); })
- .Build();
-
- await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
-
- Console.WriteLine("MQTT client subscribed to topic.");
-
- Console.WriteLine("Press enter to exit.");
- Console.ReadLine();
- }
- }
-
- public static async Task Subscribe_Topic()
- {
- /*
- * This sample subscribes to a topic.
- */
-
- 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 mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
- .WithTopicFilter(f => { f.WithTopic("mqttnet/samples/topic/1"); })
- .Build();
-
- var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
-
- Console.WriteLine("MQTT client subscribed to topic.");
-
- // The response contains additional data sent by the server after subscribing.
- response.DumpToConsole();
- }
- }
- }
|