我有这个程序使用mosquitto MQTT 库:
/*
compile using:
$ gcc -o libmosq libmosq.c -lmosquitto
*/
#include <stdio.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void connection_callback(struct mosquitto* mosq, void *obj, int rc)
{
if (rc) {
printf("connection error: %d (%s)\n", rc, mosquitto_connack_string(rc));
}
else {
printf("connection success\n");
}
}
void publish_callback(struct mosquitto* mosq, void *obj, int mid)
{
printf("this is the publish callback\n");
}
int main(int argc, char *argv[])
{
struct mosquitto *mosq = NULL;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL);
if(!mosq) {
fprintf(stderr, "Error: Out of memory.\n");
exit(1);
}
mosquitto_connect_callback_set(mosq, connection_callback);
mosquitto_publish_callback_set(mosq, publish_callback);
mosquitto_username_pw_set(mosq, "user1", "passwd1");
int resultCode = mosquitto_connect(mosq, "localhost", 1883, 60);
if (resultCode != MOSQ_ERR_SUCCESS) {
fprintf(stderr, "error calling mosquitto_connect\n");
exit(1);
}
int loop = mosquitto_loop_start(mosq);
if(loop != MOSQ_ERR_SUCCESS){
fprintf(stderr, "Unable to start loop: %i\n", loop);
exit(1);
}
char topic[] = "/testtopic";
char msg[] = "foobar";
int publish = mosquitto_publish(mosq, NULL, topic, (int) strlen(msg), msg, 0, false);
if(publish != MOSQ_ERR_SUCCESS){
fprintf(stderr, "Unable to publish: %i\n", publish);
exit(1);
}
// hang until control+C is done
sleep(1000000);
}
基本上,它连接到 MQTT 代理并在其上发布消息。
如果我运行程序,我会得到以下输出:
this is the publish callback
connection success
这意味着发布回调在连接回调之前被调用,从我的角度来看这是违反直觉的(因为事件的顺序是相反的:首先发生连接,然后发布消息从我的程序发送到 MQTT经纪人)。
这怎么可能?为什么会发生这种违反直觉的行为?谢谢!