【MQTT编程】Last will and Testament(LWT Payload设置)

导读:本篇文章讲解 【MQTT编程】Last will and Testament(LWT Payload设置),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

  MQTTClient_willOptions定义客户端的MQTT “Last Will and Testament” (LWT)设置。 如果客户端意外失去与服务器的连接,服务器将代表客户端将LWT消息发布到LWT主题。 这使其他客户端(已订阅LWT主题)可以知道该客户端已断开连接。 为了为特定客户端启用LWT功能,在将客户端连接到服务器的MQTTClient_connect()调用中使用的MQTTClient_connectOptions结构中传递了指向MQTTClient_willOptions结构的有效指针。 如果不需要LWT函数,则可以将指向MQTTClient_willOptions的指针设置为NULL。
LWT payload格式

struct{
		int len;
		const void* data;
}payload;

Data Fields

Type Data Description
char struct_id [4] The eyecatcher for this structure. must be MQTW.
int struct_version The version number of this structure. Must be 0 or 1 0 means there is no binary payload option
const char * topicName The LWT topic to which the LWT message will be published.
const char * message The LWT payload in string form.
int retained The retained flag for the LWT message
int qos The quality of service setting for the LWT message.

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "MQTTClient.h"
#define ADDRESS     "tcp://192.168.1.101:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT_Examples"
#define PAYLOAD     "Hello MQTT!"
#define QOS         1
#define TIMEOUT     10000L

#define LAST_WILL_TOPIC "Hello_Will"
#define LAST_WILL_MSG "take care yourself"

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
		MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;

    MQTTClient_deliveryToken token;
    int rc;
    MQTTClient_create(&client, ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

	will_opts.topicName = LAST_WILL_TOPIC;
	will_opts.message = LAST_WILL_MSG;
	conn_opts.will = &will_opts;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(EXIT_FAILURE);
    }
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    while (1)
    {
        MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
        printf("Waiting for up to %d seconds for publication of %s\n"
                "on topic %s for client with ClientID: %s\n",
                (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
        rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
        printf("Message with delivery token %d delivered\n", token);
        sleep(1);
    }
    
    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);
    return rc;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/116948.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!