Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection_lost is always called after keep_alive_interval timeout even if network already restored #398

Open
JohnChain opened this issue Jun 11, 2022 · 0 comments

Comments

@JohnChain
Copy link

JohnChain commented Jun 11, 2022

Here is my test step:

  1. using async_client
  2. set callback (connected and connection_lost)
  3. set keep_alive_interval to 30s
  4. set automatic_reconnect(1s, 32s)
  5. run program and connected to mqtt server
  6. disable network (eg: plugout cable),and wait for 10s
  7. enable network (eg: plugin cable) ,and the OS system network is restored within 10s

the connected callback is not call immediately, and the connection_lost callback called after timeout of keep_alive_interval(may be 1.5*30s).
I add reconnect in connection_lost, and after calling reconnect, the connected callback is called with message "automatic reconnect"

class callback : public virtual mqtt::callback{
public:
    void connected(const string& msg) {
        if(msg.compare("automatic reconnect") == 0){
            client->subscribe(sub_ctrl, QOS);
            client->subscribe(sub_result, QOS);
        }
        LOG_D("*** Connection created (%s) isconnencted:%d ***", msg.c_str(), client->is_connected());

    }
    void connection_lost(const std::string& msg) override {
        LOG_E("*** Connection Lost (%s), will reconnect ***", msg.c_str());
        client->reconnect();
    }

    void delivery_complete(mqtt::delivery_token_ptr tok) override {
        // LOG_D("[%ld]Delivery complete: %d", newtimeus, (tok ? tok->get_message_id() : -1));
    }

    void message_arrived(mqtt::const_message_ptr msg) {
	const std::string &sub_msg = msg->get_payload_str();
	// dispatch msg
    }
private:
};

int prepareMqttClient(mqtt::async_client &client, callback &cb, const char* username, const char* passwd){
    client->set_callback(cb);
    // Build the connect options, including SSL and a LWT message.
    auto sslopts = mqtt::ssl_options_builder()
                        .trust_store(strlen(cert_path)? cert_path:TRUST_STORE)
                        //.key_store(KEY_STORE)  /* enabled when Mutual Authentication*/
                        .error_handler([](const std::string& msg) {
                            LOG_E("SSL Error: %s", msg.c_str());
                        })
                        .finalize();


    auto willmsg = mqtt::message(LWT_TOPIC, LWT_PAYLOAD, QOS, true);
    auto connopts = mqtt::connect_options_builder()
                        .clean_session(true)
                        .connect_timeout(std::chrono::seconds(10))
                        .keep_alive_interval(std::chrono::seconds(30))
                        .max_inflight(1024)
                        .automatic_reconnect(std::chrono::seconds(1), std::chrono::seconds(32))
                        .user_name(username)
                        .password(passwd)
                        .will(std::move(willmsg))
                        .ssl(std::move(sslopts))
                        .finalize();
    // connect
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant