diff --git a/include/mqttclient.h b/include/mqttclient.h index 0f1d7db..7d9e666 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -49,12 +49,12 @@ public: Subscription( const std::string &topic, int qos, const std::function& call_back ) : m_topic( topic ) , m_qos( qos ) - , m_call_back(call_back ) + , m_call_back(const_cast&>(call_back )) {} std::string getTopic() const { return m_topic; } int getQoS() const { return m_qos; } - std::function& getCallBack() const { return m_call_back; } + std::function& getCallBack() const { return m_call_back; } private: std::string m_topic; diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index b3276b9..d39e536 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -416,6 +416,18 @@ void MqttClient::connectionStatusChanged(const std::string& id, ConnectionStatus bool resubscribe = ( StateEnum::Good == newState ); if (resubscribe) { + // First activate pending subscriptions + { + OSDEV_COMPONENTS_LOCKGUARD(m_subscriptionMutex); + LogDebug( "[MqttClient::connectionsStatusChanged]", std::string( m_clientId + " - Number of pending subscriptions : " + std::to_string(m_deferredSubscriptions.size() ) ) ); + while( m_deferredSubscriptions.size() > 0 ) + { + auto subscription = m_deferredSubscriptions.at( 0 ); + this->subscribe( subscription.getTopic(), subscription.getQoS(), subscription.getCallBack() ); + m_deferredSubscriptions.erase( m_deferredSubscriptions.begin() ); + } + } + LogDebug( "[MqttClient::connectionStatusChanged]", std::string( m_clientId + " - Resubscribing..." ) ); {