來源:互聯網
上載者:User
關鍵字
雲計算
MQTT
pmqtt協定
paho庫
rabbit-mqtt+pmqtt協定+paho庫嘗鮮(ubuntu16.04)。
步驟
1 下載安裝rabiit-mq mqtt-server
就用deb包
另需要
sudo apt-get install erlang
2 啟動
sudo /etc/init.d/rabbitmq-server restart
3 mqtt-3.1協定支援
rabbitmq-plugins enable rabbitmq_mqtt
4 開啟動web管理
rabbitmq-plugins enable rabbitmq_management
5 下載編譯paho cpp
6 運行mqtt PUB/SUB的例子,用c++封裝成hapoWapper, 結合protoc buf
編譯: g++ main.cpp -o -lpaho-mqtt3c --std=c++11 代碼
用c++封裝了下hapo的c調用庫-hapoWapper.h
main.cpp
#include #include #include "hapoWapper.h" using std::cout; using std::endl; using std::shared_ptr; #define ADDRESS "127.0.0.1:1883" #define CLIENTID "58daf13e258951000ab2b38f" #define TOPIC1 "Good man" #define TOPIC2 "Nor mal man" const char* TOKEN = "YBljNeKVdOVYPFJo7EjztFVoya94XV4GBuvB9U85KJY="; void fdelivered(void *coNtext, MQTTClient_deliveryToken dt) { printf("Message with token value %d delivery confirmed\n", d t); } int fmsgarrvd(void *coNtext, char *topicName, int topicLen, MQTTClient_message *message) { int i; char* payloadptr; prin tf("Message arrived\n"); printf(" topic: %s\n", topicName); printf(" message: "); if(!strcmp(topicName,TOPIC1)) { printf("Got token size = %d, %s\n",message->payloadlen , (char*)message->payload) ; payloadptr = (char*)message->payload; for(i=0; ipayloadlen; i++) { putchar(*payloadptr++); } putchar('\n'); MQTTClient_freeMessage(&message); MQTTClient_free(topicName); return 1; } void fconnlost(void *coNtext, char *cause) { printf("\nConnection lost\n"); printf(" cause: %s\n", cause); } int main(int argc, char* argv[]) { MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; conn_op ts.keepAliveInterval = 20; conn_opts.cleansession = 1; clientParams paras(ADDRESS,CLIENTID,conn_opts); cbGroups cbs(fconnlost,fdelivered,fmsgarrvd); pahoWapper wap(paras); wap.connect(cbs); printf("Subcribe topic:%s %s\n",TOPIC1,TOPIC2); wap.subscribe(TOPIC2,1); wap.subscribe(TOPIC1,1); printf("Publish with topic:%s\n",TOPIC2); auto words = "coooooooool"; wap.publish(TOPIC2,words,strlen(words),1); char ch; do { ch =getchar(); } while(ch !='Q' && ch != 'q'); return 0; }
hapoWapper.h
#ifndef HAPO_WAPPER_H #define HAPO_WAPPER_H #include "MQTTClient.h" #include #include typedef void (*connlost)(void* ,cha r*); typedef void (*delivered)(void *coNtext, MQTTClient_deliveryToken dt); typedef int (* msgarrvd)(void *coNtext, char *topicName, int topicLen, MQTTClient_message *message); class clientParams { public: clientParams(){}; ~clientParams(){}; const char* _server_uri; const char* _client_id; MQTTClient_connectOptions _conn_opts; clientParams(const char*a, const char* b, const MQTTClient_connectOptions & o): _server_uri(a),_client_id(b),_conn_ opts(o){}; clientParams & operator=(const clientParams & cp) { if(&cp == this) return *this; _server_uri = cp._server_uri ; _client_id = cp._client_id; _conn_opts = cp._conn_opts; return *this; }; }; class cbGroups { public: connlost connect_cb; delivered delivered_cb; msgarrvd msgarrvd_cb; cbGroups(connlost c, delivered d, msgarrvd m): connect_cb(c),delivered_cb(d),msgarrvd_cb(m) {}; }; class pahoWapper { public: pahoWapper(){}; ~pahoWapper(){}; pahoWapper(const clientParams & paras) { _paras = paras; printf("client %s access %s\n",_paras._client_id, _paras._ser ver_uri); MQTTClient_create(&_client, _paras._server_uri, _paras._client_id, MQTTCLIENT_PERSISTENCE_NONE, Null); }; int connect(const cbGroups & cbs) { int rc; MQTTClient_setCallbacks(_client, Null, cbs.connect_cb, cbs.msgarrvd_cb, cbs.delivered_cb); if ((rc = MQTTClient_connect(_client, &(_paras._conn_opts))) != MQTTCLIENT_SUCCESS) { printf("Failed to connect, retu rn code %d\n", rc); exit(-1); } return rc; }; void disconnect() { MQTTClient_disconnect(_client, 10000); MQTTClient_destroy(&_client); }; void publish(const char* topic, const void* payload, int len , int qos_lvl) { MQTTClient_message pubmsg = MQTTClient_messa ge_initializer; MQTTClient_deliveryToken token; pubmsg.payload = (void*)payload; pubmsg.payloadlen = len; pubmsg.qos = qos_lvl; pubmsg.retained = 0; MQTTClient_publishMessage(_client, topic, &pubmsg, &token); printf("Waiting for publication of %s\n" "on topic %s for client with ClientID: %s\n", (char*)payload, topic, _paras._clie nt_id); }; void subscribe(const char* topic,int qos_lvl) { MQTTClient_subscribe(_client, topic, qos_lvl); }; private: MQTTClient _client; clientParams _paras; }; #endif