使用Mosquito订阅返回消息
Return message subscribe using Mosquitto?
我有下面的Mosquitto代码,这些代码订阅了c++中的特定主题。
main.cpp
/*
* main.cpp
*
* Created on: Jul 28, 2016
* Author: nilav
*/
#include <iostream>
#include "myMosq.h"
#include <string.h>
#include <unistd.h>
using namespace std;
int main() {
myMosq *mosq;
mosq = new myMosq("unique","topic", "localhost",1883);
int res;
while(1) {
char tstr[500] ;
// cin.getline(tstr,sizeof(tstr));
sleep(2);
// mosq->send_message(tstr);
mosq->receive_message(tstr);
res = mosq->loop(); // Keep MQTT connection
if (res)
mosq->reconnect();
}
}
myMosq.h
/*
* myMosq.h
*
* Created on: Jul 28, 2016
* Author: nilav
*/
#ifndef MYMOSQ_H_
#define MYMOSQ_H_
#include <mosquittopp.h>
#include <mosquitto.h>
class myMosq : public mosqpp::mosquittopp
{
private:
const char * host;
const char * id;
const char * topic;
int port;
int keepalive;
void on_connect(int rc);
void on_disconnect(int rc);
void on_subscribe(int mid, int qos_count, const int *granted_qos);
public:
myMosq(const char *id, const char * _topic, const char *host, int port);
~myMosq();
bool send_message(const char * _message);
bool receive_message(const char * _message);
};
#endif
myMosq.cpp
#include <cstdio>
#include <cstring>
#include <iostream>
#include "myMosq.h"
#include <mosquittopp.h>
using namespace std;
myMosq::myMosq(const char * _id,const char * _topic, const char * _host, int _port) : mosquittopp(_id)
{
mosqpp::lib_init(); // Mandatory initialization for mosquitto library
this->keepalive = 60; // Basic configuration setup for myMosq class
this->id = _id;
this->port = _port;
this->host = _host;
this->topic = _topic;
connect_async(host, // non blocking connection to broker request
port,
keepalive);
loop_start(); // Start thread managing connection / publish / subscribe
};
myMosq::~myMosq() {
loop_stop(); // Kill the thread
mosqpp::lib_cleanup(); // Mosquitto library cleanup
}
//bool myMosq::send_message(const char * _message)
// {
// int ret = publish(NULL,this->topic,strlen(_message),_message,1,false);
// cout << ret;
// return ( ret == MOSQ_ERR_SUCCESS );
// }
bool myMosq::receive_message(const char * message)
{
int set = subscribe(NULL, this->topic,2);
return set;
}
void myMosq::on_disconnect(int rc) {
std::cout << ">> myMosq - disconnection(" << rc << ")" << std::endl;
}
void myMosq::on_connect(int rc)
{
if ( rc == 0 ) {
std::cout << ">> myMosq - connected with server" << std::endl;
} else {
std::cout << ">> myMosq - Impossible to connect with server(" << rc << ")" << std::endl;
}
}
void myMosq::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
std::cout << ">> subscription succeeded (" << mid << ") " << std::endl;
printf("Subscription succeeded.n");
}
现在,当我从ubuntu 的终端发出以下命令时
mosquitto_pub-h localhost-t"主题"-m"你好MQTT"
程序输出中不显示任何内容。但我想要一个代码来显示订阅时在特定主题中生成的特定消息。任何帮助都将不胜感激。
设置bool myMosq::receive_message(const char * message)
函数的方式存在错误。就MQTT而言,您最初订阅一个主题一次,然后使用mosquitopp包装器提供的循环函数来检查相应主题的更改数据。现在,您只需要指定void on_message(const struct mosquitto_message* message)
回调应该发生什么,并根据使用的数据类型获取和格式化消息对象。
例如,为了获取某种与字符相关的数据,我的重载实现如下所示:
void MQTTSubscriber::on_message(const mosquitto_message* message)
{
cout << "Subscriber " << id << " received message of topic: " << message->topic << " Data: " << reinterpret_cast<char*>(message->payload) << "n";
}
connect_async()完成了建立和保持代理连接的所有工作,loop_start()函数处理线程独立的回调函数。
谨致问候,Richard
- glad 导致 glfwSwapBuffers 返回错误消息
- 从函数(不抛出函数)返回异常以进行消息格式化?
- 不断收到错误消息,并非所有控制路径都返回值
- 当模板返回类型阻止其实例化时,如何向用户提供漂亮的static_assert消息?
- C++ grpc::experimental:interceptor 如何从自定义拦截器返回状态和消息
- 可以消息:: getDescriptor()返回null
- 如何将没有终止字符和大小的返回消息一起使用提升async_read?
- git_remote_fetch返回错误并显示消息:"there is no TLS stream available"
- Win32 消息循环 - 我应该中断还是返回
- 哪种方式将 HHOOK 消息返回到系统更好
- 主对话框在命令消息处理程序返回之前销毁
- InternetGetLastResponseInfo返回奇怪的字符,而不是错误消息
- 只有1条错误消息:1d返回1个退出状态,C++
- 带有 MSG_PEEK 的 recv() 显示完整消息,但正常返回'would block'
- C++ 有没有办法循环遍历一个向量,只有在完全搜索后才返回一条消息
- Windows 错误消息返回代码而不是消息
- 更丰富的错误返回消息用于提升::精神::qi 解析
- 如果引发异常,则使外部C++函数返回一条消息
- 创建进程并在进程返回消息后运行代码(c++)
- 使用Mosquito订阅返回消息