2

I connected to MQTT broker using Mosquitto C client libraray.

I used below code for connection.

ret = mosquitto_connect (mosq, MQTT_HOSTNAME, MQTT_PORT, 0);

After connecting to broker I stopped the broker service.

Now I tried to publish message using below code.

ret = mosquitto_publish (mosq, NULL,topic, strlen (text), text, 1, 1);

Eventhough the broker is running, mosquitto_publish API returns success. When calling mosquitto_publish API second time, it returns 14.

Why mosquitto_publish returns success evethough the broker is running?How to fix this issue?

Thanks in advance.

4

1 回答 1

2

与 一起使用时mosquitto_start(),该mosquitto_publish()函数完全是异步的。它所做的只是向队列中添加一条新消息并唤醒网络线程。如果客户端上次尝试与代理通信时一切正常,那么我们无法知道连接已断开。当您调用mosquitto_publish()它时,它只能返回成功,除非有任何其他错误。当客户端尝试发送该发布时,它发现网络已关闭,因此任何后续发布都将返回相应的错误。

于 2017-03-06T10:39:37.653 回答