77// ReSharper disable InconsistentNaming
88// ReSharper disable UnusedMember.Local
99
10- using MQTTnet . Extensions . TopicTemplate ;
1110using MQTTnet . Packets ;
1211using MQTTnet . Protocol ;
1312using MQTTnet . Samples . Helpers ;
@@ -16,8 +15,6 @@ namespace MQTTnet.Samples.Client;
1615
1716public 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
0 commit comments