Mosquitto 发布者不发布任何消息 [英] Mosquitto publisher does not publish any message

查看:190
本文介绍了Mosquitto 发布者不发布任何消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to develop a C code using mosquitto lib to publish message on a mosquitto broker over TLS. I configured the TLS on mosquitto side and it worked fine. I was able to send and receive messages using mosquitto_pub and mosquitto_sub.

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

However, when I tried to publish a message using my C code, it does not work. Apparently, the code connects fine and sends the message, there is no error but the subscriber does not read anything.

以下是我使用的发布商代码:

Below is the publisher code I am using:

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

怎么了?

谢谢,毛罗

推荐答案

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

You should look at the loop*() set of functions, these are needed to process the background network traffic. publish() is not a blocking call.

这篇关于Mosquitto 发布者不发布任何消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆