diff --git a/src/providers/liveupdates/BasicPubSubClient.hpp b/src/providers/liveupdates/BasicPubSubClient.hpp index 31532c1dd..27b1a798c 100644 --- a/src/providers/liveupdates/BasicPubSubClient.hpp +++ b/src/providers/liveupdates/BasicPubSubClient.hpp @@ -150,6 +150,15 @@ protected: return this->started_.load(std::memory_order_acquire); } + /** + * @brief Will be called when the clients has been requested to stop + * + * Derived classes can override this to do their own shutdown behaviour + */ + virtual void stopImpl() + { + } + liveupdates::WebsocketClient &websocketClient_; private: @@ -164,6 +173,8 @@ private: { assert(this->isStarted()); this->started_.store(false, std::memory_order_release); + + this->stopImpl(); } liveupdates::WebsocketHandle handle_; diff --git a/src/providers/seventv/eventapi/Client.cpp b/src/providers/seventv/eventapi/Client.cpp index f266478ce..465cbc03d 100644 --- a/src/providers/seventv/eventapi/Client.cpp +++ b/src/providers/seventv/eventapi/Client.cpp @@ -13,6 +13,8 @@ Client::Client(liveupdates::WebsocketClient &websocketClient, : BasicPubSubClient(websocketClient, std::move(handle)) , lastHeartbeat_(std::chrono::steady_clock::now()) , heartbeatInterval_(heartbeatInterval) + , heartbeatTimer_(std::make_shared( + this->websocketClient_.get_io_service())) { } @@ -23,6 +25,11 @@ void Client::onConnectionEstablished() this->checkHeartbeat(); } +void Client::stopImpl() +{ + this->heartbeatTimer_->cancel(); +} + void Client::setHeartbeatInterval(int intervalMs) { qCDebug(chatterinoSeventvEventAPI) @@ -54,14 +61,13 @@ void Client::checkHeartbeat() auto self = std::dynamic_pointer_cast(this->shared_from_this()); - runAfter(this->websocketClient_.get_io_service(), this->heartbeatInterval_, - [self](auto) { - if (!self->isStarted()) - { - return; - } - self->checkHeartbeat(); - }); + runAfter(this->heartbeatTimer_, this->heartbeatInterval_, [self](auto) { + if (!self->isStarted()) + { + return; + } + self->checkHeartbeat(); + }); } } // namespace chatterino::seventv::eventapi diff --git a/src/providers/seventv/eventapi/Client.hpp b/src/providers/seventv/eventapi/Client.hpp index 11683edcf..511488327 100644 --- a/src/providers/seventv/eventapi/Client.hpp +++ b/src/providers/seventv/eventapi/Client.hpp @@ -24,6 +24,7 @@ public: protected: void onConnectionEstablished() override; + void stopImpl() override; private: void checkHeartbeat(); @@ -32,6 +33,7 @@ private: lastHeartbeat_; // This will be set once on the welcome message. std::chrono::milliseconds heartbeatInterval_; + std::shared_ptr heartbeatTimer_; friend SeventvEventAPI; };