pahomqtt_c++ demo qt环境编译

#define MQTTCLIENT_QOS2 1

#include 

#include "MQTTClient.h"

#define DEFAULT_STACK_SIZE -1

#include "linux.cpp"

int arrivedcount = 0;



void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;

    printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n", 
		++arrivedcount, message.qos, message.retained, message.dup, message.id);
    printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
}


int main(int argc, char* argv[])
{   
    IPStack ipstack = IPStack();  //new一个连接对象
    float version = 0.3;
    const char* topic = "mbed-sample";   //订阅的主题

    MQTT::Client client = MQTT::Client(ipstack);  //new一个mqtt连接对象
    
    const char* hostname = "iot.eclipse.org";          //域名
    int port = 1883;                                   //端口号
    printf("Connecting to %s:%d\n", hostname, port);
    int rc = ipstack.connect(hostname, port);          //建立tcp连接到iot.eclipse.org服务器
	if (rc != 0)
	    printf("rc from TCP connect is %d\n", rc);
 
	printf("MQTT connecting\n");
                                                                      //详见 MQTTPacket_connectData 定义
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer; //初始化一个mqtt消息结构体,并且写入固定头
    data.MQTTVersion = 3;                                             //定义mqtt版本
    data.clientID.cstring = (char*)"mbed-icraggs";                    //定义客户端标识符
   //data.username.cstring;                                           //定义用户名
   //data.password.cstring;                                           //定义密码
    data.keepAliveInterval = 10;                                      //定义时间间隔内
    data.cleansession = 1;                                            //定义会话清除标识

    rc = client.connect(data);                       //建立mqtt连接
    if (rc != 0)
	    printf("rc from MQTT connect is %d\n", rc);
	printf("MQTT connected\n");
    
    rc = client.subscribe(topic, MQTT::QOS2, messageArrived);  //订阅主题 , 服务质量 , 以及传入接收函数指针
    if (rc != 0)
        printf("rc from MQTT subscribe is %d\n", rc);

    MQTT::Message message;                //初始化mqtt信息变量message

    // QoS 0
    char buf[100];
    sprintf(buf, "Hello World!  QoS 0 message from app version %f", version);
    message.qos = MQTT::QOS0;             //定义服务质量
    message.retained = false;             //是否任何新的订阅者都会收到这条消息
    message.dup = false;                  //是否是重发的消息 发送者若在一段时间内接收不到PUBACK消息,发送者需要打开DUB标记为1,然后重新发送PUBLISH消息
    message.payload = (void*)buf;         //消息内容
    message.payloadlen = strlen(buf)+1;   //消息内容长度
    rc = client.publish(topic, message);  //发布消息
	if (rc != 0)
		printf("Error %d from sending QoS 0 message\n", rc);
    else while (arrivedcount == 0)        //等待订阅消息接收
        client.yield(100);                //延时?
        
    // QoS 1
	printf("Now QoS 1\n");
    sprintf(buf, "Hello World!  QoS 1 message from app version %f", version);
    message.qos = MQTT::QOS1;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, message);
	if (rc != 0)
		printf("Error %d from sending QoS 1 message\n", rc);
    else while (arrivedcount == 1)
        client.yield(100);
        
    // QoS 2
    sprintf(buf, "Hello World!  QoS 2 message from app version %f", version);
    message.qos = MQTT::QOS2;
    message.payloadlen = strlen(buf)+1;
    rc = client.publish(topic, message);
	if (rc != 0)
		printf("Error %d from sending QoS 2 message\n", rc);
    while (arrivedcount == 2)
        client.yield(100);
    
    rc = client.unsubscribe(topic);  //取消订阅
    if (rc != 0)
        printf("rc from unsubscribe was %d\n", rc);
    
    rc = client.disconnect();        //断开mqtt
    if (rc != 0)
        printf("rc from disconnect was %d\n", rc);
    
    ipstack.disconnect();            //断开tcp
    
    printf("Finishing with %d messages received\n", arrivedcount);
    
    return 0;
}

 

源码:

链接:https://pan.baidu.com/s/12ebMkdV9qYSpH6G0Tpyecg 
提取码:609w 
复制这段内容后打开百度网盘手机App,操作更方便哦

你可能感兴趣的:(日记)