0

我一直在尝试使用 mosquitto lib 开发 C 代码,以通过 TLS 在 mosquitto 代理上发布消息。我在 mosquitto 端配置了 TLS,它运行良好。我能够使用 mosquitto_pub 和 mosquitto_sub 发送和接收消息。

但是,当我尝试使用我的 C 代码发布消息时,它不起作用。显然,代码连接正常并发送消息,没有错误但订阅者没有读取任何内容。

以下是我正在使用的发布者代码:

ReportSender::ReportSender()
{
    mosquitto_lib_init();

    mosquitoStruct = mosquitto_new (NULL, true, NULL);

    mosquitto_tls_opts_set(mosquitoStruct, 1, NULL, NULL);

    mosquitto_tls_set(mosquitoStruct, "~/temp/keys/secondAttempt/server.crt", NULL, NULL, NULL, NULL);

    mosquitto_tls_insecure_set(mosquitoStruct, false);

    mosquitto_connect_callback_set(mosquitoStruct, connect_cb);
    mosquitto_publish_callback_set(mosquitoStruct, publish_cb);
    mosquitto_log_callback_set(mosquitoStruct, log_cb);

    mosquitto_connect (mosquitoStruct, MQTT_BROKER, MQTT_PORT, 0);

    const char *reportRef = "Hello Word!";

    // Publish the message to the topic
    mosquitto_publish (mosquitoStruct, NULL, MQTT_TOPIC,
              strlen(reportRef), reportRef, 0, false);

    sleep (20);
}

订阅者是:

mosquitto_sub -h 192.168.56.101 -p 8883 -t "#" -v --cafile server.crt

怎么了?

谢谢,毛罗

4

1 回答 1

1

您应该查看 loop*() 函数集,这些函数是处理后台网络流量所必需的。publish() 不是阻塞调用。

于 2018-04-30T09:45:40.397 回答