Series Tutorial Directory
(1) Connect to Alibaba Cloud IOT
(2) Set Delegate Events
(3) Report Properties
(4) SDK Documentation: Properties, Methods, Delegates, Classes
http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/AliIOTXFclient-dll类库&response
Download the three libraries and include them at the top to use them.
using AliIOTXFClient;
Sample download link
http://pan.whuanle.cn/index.php?dir=uploads/阿里云IOT/demo示例
This chapter's example AliIOTXF.Twe
Lifecycle
Event Types
Currently, five delegate events have been set.
XFMQTT.ConnectionClosedEventHandler
XFMQTT.PubedEventHandler
XFMQTT.PubEventHandler
XFMQTT.SubedEventHandler
XFMQTT.UnSubedEventHandler
Delegate | Description |
XFMQTT.PubEventHandler | Subscription callback - when a message is received from the server |
XFMQTT.PubedEventHandler | Triggered when QOS=1 or 2 and a subscription is received |
XFMQTT.SubedEventHandler | When publishing a Topic to the server |
XFMQTT.UnSubedEventHandler | When failing to publish a Topic to the server |
XFMQTT.ConnectionClosedEventHandler | Connection closed |
MQTT is a reliable message-pushing protocol, and QOS ensures that messages must be completely pushed to the other side. You can search for specific information about QOS on Baidu~
- QOS = 0, at most once
- QOS = 1, at least once
- QOS = 2, exactly once
This represents the reliability of message pushing. When QOS = 1, under network fluctuations, there may be multiple duplicate push events.
Using Default Delegate Methods:
// Use default delegate events client.UseDefaultEventHandler();
Through the previous article, the program can now run, connect to the Alibaba Cloud server, and subscribe and publish messages.
These operations will trigger events, and by using the UseDefaultEventHandler() method, the default method is bound to the events.
Custom Event Methods
Remove this line of code.
client.UseDefaultEventHandler();
Include at the top
using uPLibrary.Networking.M2Mqtt.Messages;
Note, the parameters of these five delegate methods are different
XFMQTT.PubEventHandler
Subscription callback - triggered when a message is received from the server
Delegate method is as follows
Delegate method(object sender, MqttMsgPublishEventArgs e) {}</span></pre>
MqttMsgPublishEventArgs is an object related to receiving messages, its properties are as follows:
// message topic private string topic; // message data private byte[] message; // duplicate delivery private bool dupFlag; // quality of service level private byte qosLevel; // retain flag private bool retain;
The default delegate method provided by the SDK
public void Default_PubEventHandler(object sender, MqttMsgPublishEventArgs e) { // handle message received string topic = e.Topic; string message = Encoding.ASCII.GetString(e.Message); Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("get topic message, Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("topic: " + topic); Console.WriteLine("get message :\n" + message); }
Now let's write a custom delegate method
The content inside can be anything, and the method name is arbitrary
public static void 收到消息(object sender, MqttMsgPublishEventArgs e) { Console.WriteLine("topic name:" + e.Topic); Console.WriteLine("receiving time:" + DateTime.Now.ToLongDateString()); Console.WriteLine("message content:\n" + Encoding.ASCII.GetString(e.Message)); }
Add the following in the position where `client.UseDefaultEventHandler();` is deleted
client.PubEventHandler += 收到消息;
XFMQTT.PubedEventHandler
Triggered when subscription is received with QOS=1 or 2
Prototype
Delegate method(object sender, MqttMsgPublishedEventArgs e) {}</span></pre>
MqttMsgPublishedEventArgs Properties
// message identifier ushort messageId;</span><span style="color: #008000;">//</span><span style="color: #008000;"> published flag</span> <span style="color: #0000ff;">bool</span> isPublished;</pre>
Default method provided by the SDK
public void Default_PubedEventHandler(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("published, Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId + " Is Published: " + e.IsPublished); }
We will create a custom method, the name can be anything
public static void 重复收到消息(object sender, MqttMsgPublishedEventArgs e) { Console.WriteLine("receiving time: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("message id: " + e.MessageId + " Is Published: " + e.IsPublished); }
Add the following
client.PubedEventHandler += 重复收到消息;
XFMQTT.SubedEventHandler
Triggered when publishing a Topic to the server
Prototype
void 发布消息时(object sender, MqttMsgSubscribedEventArgs e)
MqttMsgSubscribedEventArgs Properties
/// <summary> /// Message identifier /// </summary> public ushort MessageId {get;set;}</span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> List of granted QOS Levels </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">byte</span>[] GrantedQoSLevels {<span style="color: #0000ff;">get</span>;<span style="color: #0000ff;">set</span><span style="color: #000000;">;} </span><span style="color: #008000;">//</span><span style="color: #008000;"> message identifier</span> <span style="color: #0000ff;">ushort</span><span style="color: #000000;"> messageId; </span><span style="color: #008000;">//</span><span style="color: #008000;"> granted QOS levels</span> <span style="color: #0000ff;">byte</span>[] grantedQosLevels;</pre>
SDK default implementation
public void Default_SubedEventHandler(object sender, MqttMsgSubscribedEventArgs e) { Console.WriteLine("- - - - - - - - - - "); Console.WriteLine("Sub topic, Date: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("MessageId: " + e.MessageId); Console.WriteLine("List of granted QOS Levels: " + string.Join(", " e.GrantedQoSLevels));
Encoding.UTF8.GetString(e.GrantedQoSLevels));
}</span></pre>
</div>
<p>We write custom code, method names are arbitrary</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public static</span> <span style="color: #0000ff;">void</span> PublishMessage(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgSubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message sent to server</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Send Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">QOS is: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.UTF8.GetString(e.GrantedQoSLevels));
}</span></pre>
</div>
<p>Add:</p>
<div class="cnblogs_code">
<pre>client.SubscribedEventHandler += PublishMessage;</pre>
</div>
<p> </p>
<h3>XFMQTT.UnsubscribedEventHandler</h3>
<p>Triggered when failing to publish Topic to the server</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">void</span> SendFailure(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs )
{
}</span></pre>
</div>
<p>MqttMsgUnsubscribedEventArgs Properties</p>
<div class="cnblogs_code">
<pre> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
<span style="color: #808080;">///</span><span style="color: #008000;"> Message identifier
</span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">ushort</span><span style="color: #000000;"> MessageId
{
</span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.messageId; }
</span><span style="color: #0000ff;">internal</span> <span style="color: #0000ff;">set</span> { <span style="color: #0000ff;">this</span>.messageId =<span style="color: #000000;"> value; }
}</span></pre>
</div>
<p>SDK Code</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Default_UnsubscribedEventHandler(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">- - - - - - - - - - </span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sub topic error, Date: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">MessageId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
}</span></pre>
</div>
<p> </p>
<p>We write custom code</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public static</span> <span style="color: #0000ff;">void</span> SendFailure(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message send failed </span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Failed message MessageId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
}</span></pre>
</div>
<p>Add:</p>
<div class="cnblogs_code">
<pre>client.UnsubscribedEventHandler += SendFailure;</pre>
</div>
<h3>XFMQTT.ConnectionClosedEventHandler</h3>
<p>Triggered when the connection is closed</p>
<p> SDK Code</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Default_ConnectionClosedEventHandler(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">- - - - - - - - - - </span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Connect Closed error, Date: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
}</span></pre>
</div>
<p>Custom code</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">public static</span> <span style="color: #0000ff;">void</span> Disconnect(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Date disconnect...</span>
Disconnected: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
}</span></pre>
</div>
<p>Add</p>
<div class="cnblogs_code">
<pre>client.ConnectionClosedEventHandler += disconnected;</pre>
</div>
<p> </p>
<p>Complete Code</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Create connection object</span>
XFMQTT client = <span style="color: #0000ff;">new</span> XFMQTT(<span style="color: #800000;">"</span><span style="color: #800000;">a1BiPoNawLI</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Raspberry</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Initialize client configuration</span>
client.Init(<span style="color: #800000;">"</span><span style="color: #800000;">2NOaBeqXcIzLQEhlJFEfKbWeug0o3m0c</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">cn-shanghai</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Topic to subscribe</span>
<span style="color: #0000ff;">string</span>[] topic = { client.CombineHeadTopic(<span style="color: #800000;">"</span><span style="color: #800000;">PubData</span><span style="color: #800000;">"</span><span style="color: #000000;">) };
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Use custom delegate events</span>
client.PubEventHandler +=<span style="color: #000000;"> messageReceived;
client.PubedEventHandler </span>+=<span style="color: #000000;"> messageRepeated;
client.SubedEventHandler </span>+=<span style="color: #000000;"> messagePublished;
client.UnSubedEventHandler </span>+=<span style="color: #000000;"> sendFailed;
client.ConnectionClosedEventHandler </span>+=<span style="color: #000000;"> disconnected;
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connect to the server</span>
<span style="color: #000000;"> client.ConnectMqtt(topic);
</span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">string</span> str =<span style="color: #000000;"> Console.ReadLine();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Push content to a specific Topic</span>
client.Subscribe(client.CombineHeadTopic(<span style="color: #800000;">"</span><span style="color: #800000;">SubData</span><span style="color: #800000;">"</span><span style="color: #000000;">), str);
}
Console.ReadKey();
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> messageReceived(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Topic Name:</span><span style="color: #800000;">"</span>+<span style="color: #000000;">e.Topic);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time:</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongDateString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Content:\n</span><span style="color: #800000;">"</span>+<span style="color: #000000;">Encoding.ASCII.GetString(e.Message));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> messageRepeated(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgPublishedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Receive Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> + e.MessageId + <span style="color: #800000;">"</span><span style="color: #800000;"> Is Published: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.IsPublished);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> messagePublished(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgSubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Sent to Server</span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Send Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message ID: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">QOS Level: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Encoding.UTF8.GetString(e.GrantedQoSLevels));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> sendFailed(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, MqttMsgUnsubscribedEventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Message Send Failed </span><span style="color: #800000;">"</span><span style="color: #000000;">);
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Time: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> DateTime.Now.ToLongTimeString());
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Failed Message MessageId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> e.MessageId);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> disconnected(<span style="color: #0000ff;">object</span><span style="color: #000000;"> sender, EventArgs e)
{
Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Disconnected...</span><span style="color: #800000;">"</span>);
}
</span>
。The connection has been disconnected: " + DateTime.Now.ToLongTimeString());
}
文章评论