libmosquitto 没有正确重新连接
libmosquitto does not properly reconnect
我正在 Raspbian 上试验 libmosquitto-dev 并遇到一些问题。
到目前为止,我的代码工作得非常好。我可以连接到代理,一旦主题得到更新,我的程序就会按原样打印消息。
这只是代理在连接后死亡并重新启动的时间点。
我的代码意识到连接断开并尝试重新连接。一旦经纪人重新上线,我的代码就会重新连接。但是从这以后它不会在频道上打印任何更新。
为什么不呢?我认为这会很好地赶上连接,但事实并非如此。
她是我的密码:
[...]
static int run = 1;
void connect_callback(struct mosquitto *mosq, void *obj, int result)
{
printf("connect callback, rc=%d\n", result);
}
void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
bool match = 0;
printf("got message '%.*s' for topic '%s'\n", message->payloadlen, (char*) message->payload, message->topic);
mosquitto_topic_matches_sub("Heizung", message->topic, &match);
if (match) {
printf("got message for HEIZUNG topic\n");
}
}
int main(int argc, char *argv[])
{
uint8_t reconnect = true;
char clientid[24];
struct mosquitto *mosq;
int rc = 0;
mosquitto_lib_init();
memset(clientid, 0, 24);
snprintf(clientid, 23, "mylog_%d", getpid());
mosq = mosquitto_new(clientid, true, 0);
if(mosq){
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60);
mosquitto_subscribe(mosq, NULL, "Heizung", 0);
// rc = mosquitto_loop_forever(mosq,20,5); // Tried with this function but same issue.
while(run){
rc = mosquitto_loop(mosq, -1, 1);
if(run && rc){
printf("connection error!\n");
sleep(10);
mosquitto_reconnect(mosq);
}
}
mosquitto_destroy(mosq);
}
mosquitto_lib_cleanup();
return rc;
}
我看到的输出如下:
connect callback, rc=0
got message 'ON1' for topic 'Heizung'
got message for Heizung topic
got message 'ON2' for topic 'Heizung'
got message for Heizung topic
got message 'ON3' for topic 'Heizung'
got message for Heizung topic
connection error!
connect callback, rc=0
您看到连接错误(发生“systemctl stop mosquitto”的位置)。一旦代理再次回来,您就会看到重新连接似乎是成功的。但是它不会打印在代理返回后订阅者发送的任何新消息。 运行 mosquitto_sub 命令并行查看所有消息!
知道这里出了什么问题吗?
非常感谢!
/KNEBB
将对 mosquitto_subscribe
的调用移动到 connect_callback
,这样它将在重新连接时被调用。
由于您连接时将 CleanSession 标志设置为 true
,因此每次重新连接时将不会有持久会话,因此代理将不知道要保留订阅。
我正在 Raspbian 上试验 libmosquitto-dev 并遇到一些问题。
到目前为止,我的代码工作得非常好。我可以连接到代理,一旦主题得到更新,我的程序就会按原样打印消息。 这只是代理在连接后死亡并重新启动的时间点。 我的代码意识到连接断开并尝试重新连接。一旦经纪人重新上线,我的代码就会重新连接。但是从这以后它不会在频道上打印任何更新。
为什么不呢?我认为这会很好地赶上连接,但事实并非如此。
她是我的密码:
[...]
static int run = 1;
void connect_callback(struct mosquitto *mosq, void *obj, int result)
{
printf("connect callback, rc=%d\n", result);
}
void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
{
bool match = 0;
printf("got message '%.*s' for topic '%s'\n", message->payloadlen, (char*) message->payload, message->topic);
mosquitto_topic_matches_sub("Heizung", message->topic, &match);
if (match) {
printf("got message for HEIZUNG topic\n");
}
}
int main(int argc, char *argv[])
{
uint8_t reconnect = true;
char clientid[24];
struct mosquitto *mosq;
int rc = 0;
mosquitto_lib_init();
memset(clientid, 0, 24);
snprintf(clientid, 23, "mylog_%d", getpid());
mosq = mosquitto_new(clientid, true, 0);
if(mosq){
mosquitto_connect_callback_set(mosq, connect_callback);
mosquitto_message_callback_set(mosq, message_callback);
rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60);
mosquitto_subscribe(mosq, NULL, "Heizung", 0);
// rc = mosquitto_loop_forever(mosq,20,5); // Tried with this function but same issue.
while(run){
rc = mosquitto_loop(mosq, -1, 1);
if(run && rc){
printf("connection error!\n");
sleep(10);
mosquitto_reconnect(mosq);
}
}
mosquitto_destroy(mosq);
}
mosquitto_lib_cleanup();
return rc;
}
我看到的输出如下:
connect callback, rc=0
got message 'ON1' for topic 'Heizung'
got message for Heizung topic
got message 'ON2' for topic 'Heizung'
got message for Heizung topic
got message 'ON3' for topic 'Heizung'
got message for Heizung topic
connection error!
connect callback, rc=0
您看到连接错误(发生“systemctl stop mosquitto”的位置)。一旦代理再次回来,您就会看到重新连接似乎是成功的。但是它不会打印在代理返回后订阅者发送的任何新消息。 运行 mosquitto_sub 命令并行查看所有消息!
知道这里出了什么问题吗? 非常感谢!
/KNEBB
将对 mosquitto_subscribe
的调用移动到 connect_callback
,这样它将在重新连接时被调用。
由于您连接时将 CleanSession 标志设置为 true
,因此每次重新连接时将不会有持久会话,因此代理将不知道要保留订阅。