0

我在 linux 下为 mosquitto 使用 cpp 包装器。
初始化后:

this->keepalive = 60;   
this->id = id;
this->port = port;
this->host = host;
this->topic = topic;
mosquittopp::log_init(MOSQ_LOG_ALL, MOSQ_LOG_STDOUT);
const int result = connect(host, port, keepalive);

如何发布我的自定义消息?

我以这种方式使用包装器定义的发布方法:

int publish(uint16_t *mid, const char *topic, uint32_t payloadlen=0, const uint8_t *payload=NULL, int qos=0, bool retain=false);

所以我可以这样调用:

publish(NULL, topic, strlen(message));

但是..我可以在哪里指定真正的“信息”?

4

2 回答 2

0

Here you can fine sample code for a publishing example in c++ http://www.disk91.com/2013/technology/programming/mosquitto-c-sample-code-to-publish-message/

于 2014-07-24T13:01:07.210 回答
0

您可以将其用作,

publish(NULL, topic, strlen(message), message);

有一个想法,[取自 mosquitto 示例 temp_conv]

temp_farenheit = temp_celsius*9.0/5.0 + 32.0;
snprintf(buf, 50, "%f\n", temp_farenheit);
publish(NULL, "temperature/celsius", strlen(buf), buf);
于 2016-03-26T17:13:17.477 回答