示例之libmosquittto_callback_test

callabck源代碼

#ifndef LIBMOSQUITTTO_CALLBACK_TEST_HPP
#define LIBMOSQUITTTO_CALLBACK_TEST_HPP

#include<stdio.h>
#include<stdlib.h>
#include"mosquitto.h"

int on_message(struct mosquitto* mosq,void *userData,const struct mosquitto_message *msg){
    printf("%s %s %s (%d)\n",__PRETTY_FUNCTION__,msg->topic,(const char*)msg->payload,msg->payloadlen);
}


int libmosquittto_callback_test(){
    int rc;

    mosquitto_lib_init();

    rc = mosquitto_subscribe_callback(
                on_message,
                NULL,
                "irc/#",
                0,
                "test.mosquitto.org",
                1883,
                NULL,60,true,
                NULL,NULL,
                NULL,NULL);
    if(rc){
        printf("Error:%s\n",mosquitto_strerror(rc));
    }

    mosquitto_lib_cleanup();

    return rc;
}

#endif // LIBMOSQUITTTO_CALLBACK_TEST_HPP

運行結果

Hello World!
int on_message(mosquitto*, void*, const mosquitto_message*) irc/-lrluk-crew/usercount 1 (1)
Press <RETURN> to close this window...




 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章