1
1
using MQTTnet ;
2
- using MQTTnet . Client ;
3
- using MQTTnet . Extensions . ManagedClient ;
4
- using MQTTnet . Packets ;
5
2
using System ;
6
- using System . Collections . Generic ;
7
3
8
4
namespace ExampleClient
9
5
{
@@ -12,56 +8,31 @@ internal class Program
12
8
private static async System . Threading . Tasks . Task Main ( string [ ] args )
13
9
{
14
10
var rnd = new Random ( ) ;
15
- // Setup and start a managed MQTT client.
16
- var options = new ManagedMqttClientOptionsBuilder ( )
17
- . WithAutoReconnectDelay ( TimeSpan . FromSeconds ( 5 ) )
18
- . WithClientOptions ( new MqttClientOptionsBuilder ( )
11
+ var options = new MqttClientOptionsBuilder ( )
19
12
. WithClientId ( $ "Client{ rnd . Next ( 0 , 1000 ) } ")
20
- . WithWebSocketServer ( cfg=> cfg . WithUri ( "localhost:50482/mqtt" ) )
21
- . Build ( ) )
22
- . Build ( ) ;
23
-
24
- var mqttClient = new MqttFactory ( ) . CreateManagedMqttClient ( ) ;
25
- await mqttClient . SubscribeAsync ( new List < MqttTopicFilter > { new MqttTopicFilterBuilder ( ) . WithTopic ( "MqttWeatherForecast/90210/temperature" ) . Build ( ) } ) ;
13
+ . WithWebSocketServer ( cfg=> cfg . WithUri ( "ws://localhost:5000/mqtt" ) )
14
+ . Build ( ) ;
26
15
16
+ var mqttClient = new MqttClientFactory ( ) . CreateMqttClient ( ) ;
27
17
mqttClient . ConnectedAsync += ( e ) =>
28
18
{
29
- Console . WriteLine ( $ "Connection Result : { e . ConnectResult . ResultCode } ") ;
19
+ Console . WriteLine ( $ "Connected : { e . ConnectResult . ResultCode } ") ;
30
20
return System . Threading . Tasks . Task . CompletedTask ;
31
21
} ;
32
-
33
- mqttClient . ConnectingFailedAsync += ( e ) =>
22
+ mqttClient . DisconnectedAsync += ( e ) =>
34
23
{
35
- Console . WriteLine ( $ "Connection Failed : { e . Exception } ") ;
24
+ Console . WriteLine ( $ "Disconnected : { e . Exception } ") ;
36
25
return System . Threading . Tasks . Task . CompletedTask ;
37
26
} ;
38
-
39
27
mqttClient . ApplicationMessageReceivedAsync += e =>
40
28
{
41
- Console . WriteLine ( $ "Message from { e . ClientId } : { e . ApplicationMessage . PayloadSegment . Count } bytes.") ;
29
+ Console . WriteLine ( $ "Message from { e . ClientId } : { e . ApplicationMessage . Payload . Length } bytes.") ;
42
30
return System . Threading . Tasks . Task . CompletedTask ;
43
31
} ;
44
-
45
- await mqttClient . StartAsync ( options ) ;
46
-
47
- // Publish a message on a well known topic
48
- await mqttClient . EnqueueAsync ( new ManagedMqttApplicationMessageBuilder ( ) . WithApplicationMessage ( msg =>
49
- {
50
- msg . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
51
- msg . WithPayload ( BitConverter . GetBytes ( 98.6d ) ) ;
52
- msg . WithTopic ( "MqttWeatherForecast/90210/temperature" ) ;
53
- } ) . Build ( ) ) ;
54
-
55
- // Publish a message on a topic the server doesn't explicitly handle
56
- await mqttClient . EnqueueAsync ( new ManagedMqttApplicationMessageBuilder ( ) . WithApplicationMessage ( msg =>
57
- {
58
- msg . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) ;
59
- msg . WithPayload ( BitConverter . GetBytes ( 100d ) ) ;
60
- msg . WithTopic ( "asdfsdfsadfasdf" ) ;
61
- } ) . Build ( ) ) ;
62
-
63
- // StartAsync returns immediately, as it starts a new thread using Task.Run, and so the calling thread needs
64
- // to wait.
32
+ await mqttClient . ConnectAsync ( options ) ;
33
+ await mqttClient . SubscribeAsync ( new MqttTopicFilterBuilder ( ) . WithTopic ( "MqttWeatherForecast/90210/temperature" ) . Build ( ) ) ;
34
+ await mqttClient . PublishAsync ( new MqttApplicationMessageBuilder ( ) . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) . WithPayload ( BitConverter . GetBytes ( 98.6d ) ) . WithTopic ( "MqttWeatherForecast/90210/temperature" ) . Build ( ) ) ;
35
+ await mqttClient . PublishAsync ( new MqttApplicationMessageBuilder ( ) . WithQualityOfServiceLevel ( MQTTnet . Protocol . MqttQualityOfServiceLevel . AtLeastOnce ) . WithPayload ( BitConverter . GetBytes ( 100d ) ) . WithTopic ( "asdfsdfsadfasdf" ) . Build ( ) ) ;
65
36
Console . ReadLine ( ) ;
66
37
}
67
38
}
0 commit comments