Skip to content

Commit 99e70fd

Browse files
authored
Update samples (#2216)
Separate the topic template samples from the other samples
1 parent 535a4e0 commit 99e70fd

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

Samples/Client/Client_Subscribe_Samples.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// ReSharper disable InconsistentNaming
88
// ReSharper disable UnusedMember.Local
99

10-
using MQTTnet.Extensions.TopicTemplate;
1110
using MQTTnet.Packets;
1211
using MQTTnet.Protocol;
1312
using MQTTnet.Samples.Helpers;
@@ -16,8 +15,6 @@ namespace MQTTnet.Samples.Client;
1615

1716
public static class Client_Subscribe_Samples
1817
{
19-
static readonly MqttTopicTemplate sampleTemplate = new("mqttnet/samples/topic/{id}");
20-
2118
public static async Task Handle_Received_Application_Message()
2219
{
2320
/*
@@ -42,7 +39,7 @@ public static async Task Handle_Received_Application_Message()
4239

4340
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
4441

45-
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "2")).Build();
42+
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicFilter("mqttnet/samples/topic/2").Build();
4643

4744
await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
4845

@@ -55,7 +52,7 @@ public static async Task Handle_Received_Application_Message()
5552
public static async Task Send_Responses()
5653
{
5754
/*
58-
* This sample subscribes to a topic and sends a response to the broker. This requires at least QoS level 1 to work!
55+
* This sample subscribes to a topic and sends a detailed response to the client. This requires at least QoS level 1 to work!
5956
*/
6057

6158
var mqttFactory = new MqttClientFactory();
@@ -80,7 +77,7 @@ public static async Task Send_Responses()
8077

8178
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
8279

83-
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();
80+
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicFilter("topic/test").Build();
8481

8582
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
8683

@@ -106,9 +103,10 @@ public static async Task Subscribe_Multiple_Topics()
106103
// Create the subscribe options including several topics with different options.
107104
// It is also possible to all of these topics using a dedicated call of _SubscribeAsync_ per topic.
108105
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
109-
.WithTopicTemplate(sampleTemplate.WithParameter("id", "1"))
110-
.WithTopicTemplate(sampleTemplate.WithParameter("id", "2"), noLocal: true)
111-
.WithTopicTemplate(sampleTemplate.WithParameter("id", "3"), retainHandling: MqttRetainHandling.SendAtSubscribe)
106+
.WithTopicFilter(t => t.WithTopic("topic/1").WithAtLeastOnceQoS())
107+
.WithTopicFilter(t => t.WithTopic("topic/2").WithAtMostOnceQoS())
108+
.WithTopicFilter(t => t.WithTopic("topic/3").WithNoLocal())
109+
.WithTopicFilter(t => t.WithRetainHandling(MqttRetainHandling.SendAtSubscribe))
112110
.Build();
113111

114112
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
@@ -132,7 +130,9 @@ public static async Task Subscribe_Topic()
132130

133131
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
134132

135-
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(sampleTemplate.WithParameter("id", "1")).Build();
133+
var topicFilter = mqttFactory.CreateTopicFilterBuilder().WithTopic("mqttnet/samples/topic/2").WithAtLeastOnceQoS();
134+
135+
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicFilter(topicFilter).Build();
136136

137137
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
138138

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using MQTTnet.Extensions.TopicTemplate;
6+
using MQTTnet.Protocol;
7+
using MQTTnet.Samples.Helpers;
8+
9+
// ReSharper disable UnusedType.Global
10+
// ReSharper disable UnusedMember.Global
11+
// ReSharper disable InconsistentNaming
12+
// ReSharper disable UnusedMember.Local
13+
14+
namespace MQTTnet.Samples.Client;
15+
16+
public static class Client_Subscribe_TopicTemplate_Samples
17+
{
18+
static readonly MqttTopicTemplate SampleTemplate = new("mqttnet/samples/topic/{id}");
19+
20+
public static async Task Subscribe_Multiple_Topics_With_Template()
21+
{
22+
/*
23+
* This sample subscribes to several topics in a single request.
24+
*
25+
* This sample requires the nuget 'MQTTnet.Extensions.TopicTemplate'.
26+
*/
27+
28+
var mqttFactory = new MqttClientFactory();
29+
30+
using var mqttClient = mqttFactory.CreateMqttClient();
31+
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
32+
33+
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
34+
35+
// Create the subscribe options including several topics with different options.
36+
// It is also possible to all of these topics using a dedicated call of _SubscribeAsync_ per topic.
37+
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
38+
.WithTopicTemplate(SampleTemplate.WithParameter("id", "1"))
39+
.WithTopicTemplate(SampleTemplate.WithParameter("id", "2"), noLocal: true)
40+
.WithTopicTemplate(SampleTemplate.WithParameter("id", "3"), retainHandling: MqttRetainHandling.SendAtSubscribe)
41+
.Build();
42+
43+
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
44+
45+
Console.WriteLine("MQTT client subscribed to topics.");
46+
47+
// The response contains additional data sent by the server after subscribing.
48+
response.DumpToConsole();
49+
}
50+
51+
public static async Task Subscribe_Topic_With_Template()
52+
{
53+
/*
54+
* This sample subscribes to a topic.
55+
*
56+
* This sample requires the nuget 'MQTTnet.Extensions.TopicTemplate'.
57+
*/
58+
59+
var mqttFactory = new MqttClientFactory();
60+
61+
using var mqttClient = mqttFactory.CreateMqttClient();
62+
var mqttClientOptions = new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com").Build();
63+
64+
await mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
65+
66+
var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder().WithTopicTemplate(SampleTemplate.WithParameter("id", "1")).Build();
67+
68+
var response = await mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
69+
70+
Console.WriteLine("MQTT client subscribed to topic.");
71+
72+
// The response contains additional data sent by the server after subscribing.
73+
response.DumpToConsole();
74+
}
75+
}

0 commit comments

Comments
 (0)