【问题标题】:mosquittopp: publish message to brokermosquittopp:向代理发布消息
【发布时间】:2014-07-02 15:09:04
【问题描述】:

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

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));

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

【问题讨论】:

  • 来自 mosquitto 文档:“payload:指向要发送的数据的指针。如果payloadlen > 0,这必须是一个有效的内存位置。”我会说payload 必须是一个指向有效的指针payloadlen 大小的字符串。

标签: c++ c mqtt mosquitto


【解决方案1】:

您可以在这里为 c++ http://www.disk91.com/2013/technology/programming/mosquitto-c-sample-code-to-publish-message/ 发布示例提供精细示例代码

【讨论】:

    【解决方案2】:

    你可以把它当做,

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 2013-01-02
      • 1970-01-01
      • 2013-07-23
      • 2015-03-19
      • 1970-01-01
      相关资源
      最近更新 更多