我一直在尝试使用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
有什么问题?
谢谢, 莫罗
答案 0 :(得分:1)
您应该查看循环*()函数集,这些是处理后台网络流量所必需的。 publish()不是阻塞调用。