Open skyformat99 opened 7 years ago
Hi skyformat99,
Thanks for the excellent suggestion! The "Add automatic re-connect" is already on our feature request list, but I will give it a bump up on the list. I expect this feature to be added by the end of this 2017. How urgent is the automatic reconnect requirement for using wolfMQTT?
Thanks, David Garske, wolfSSL
@dgarske Thank you for your reply!
This feature should be a very useful feature. For example, in 4G network communication, there are often network failures, and if there are automatic reconnect functions (including automatic pub and sub), it will be much more convenient.
Have any progress about it?
I'm in need of the same feature, I tried to see if I could loop the rc = client->net->write(client->net->context, buf, buf_len, timeout_ms);
function in mqtt_socket.c until a successful connection to the broker occurs but the program simply stalls when a broker is not detected, making the 'rc' variable a bit useless(unless I'm doing it wrong)
This feature is being added and should be available next week. I will post an update here when its ready. Thanks, David Garske
Hi All,
I've pushed a basic disconnect callback to a pull request here: https://github.com/wolfSSL/wolfMQTT/pull/69
The callbacks are issued from the mqtt_packet
layer and the original error code is then sent up the stack. The idea is for you to set this disconnect callback in your application and then have it trigger some some type of reconnect action. I don't think its a good idea to do the reconnect in the callback, but instead set a state or context variable that triggers this in your main thread.
I would like to see an automatic reconnect feature as the next step for this. Let me know your thoughts.
Thanks, David Garske, wolfSSL
Hi @dgarske I'm trying to do the following
How would I use this callback you added currently? Within the netwrite thread my program stalls at the 'connect' or 'write' function.
This is the function - static int NetWrite(void *context, const byte* buf, int buf_len, int timeout_ms)
inside of mqttnet.c
and this is the line
rc = (int)SOCK_SEND(sock->fd, buf, buf_len, 0);
I don't even get any value of the rc if there is an error here, it just stops here if the broker is not running so I can't do anything further up like you mentioned. What would you suggest?
I will also try to see if I get a better result when I try closing the broker once a connection has been established to the client.
any update on the automatic reconnect? is there a way to implement it ourselves?
Hi All,
The actual socket handling for connect is handled via callbacks, which the consumer of the library is responsible for setting up. If you are waiting for a message MqttClient_WaitMessage
and MQTT_CODE_ERROR_TIMEOUT
then you might consider doing a "keep-alive" much like our example here: https://github.com/wolfSSL/wolfMQTT/blob/master/examples/mqttclient/mqttclient.c#L508
If you get a response code that is not MQTT_CODE_SUCCESS
or MQTT_CODE_CONTINUE
(for non-blocking) then you would have received a socket error and you would need to reconnect the socket and call MqttClient_Connect
again.
We added a disconnect callback that you can register via MqttClient_SetDisconnectCallback
, which will tell you when a network error occurred. This allows you to set a flag indicating your client needs to reconnect. The actual socket reconnect cannot occur in this callback.
Let me know if you have any questions or issues.
Thanks, David Garske, wolfSSL
Can you add automatic connect function? thanks. For instance, When the broker restarts, ping/pong test cannot auto connect to the broker server.
like as: Recently, I learned about paho. MQTT. C, and found the function as: void connectionLost(void context, char cause) url: https://github.com/eclipse/paho.mqtt.c/blob/master/src/samples/paho_c_pub.c
so, Maybe the following function names are more appropriate: typedef void (lmqtt_client_on_connect_lost)(void , lmqtt_connect_t *, int);
disconnect: Disconnect actively by itself connect_lost: Passive disconnect.