Disconnect Twitch chat and event connection on plugin stop
Some checks failed
debian-build / build (push) Has been cancelled
Push to master / Check Formatting 🔍 (push) Has been cancelled
Push to master / Build Project 🧱 (push) Has been cancelled
Push to master / Create Release 🛫 (push) Has been cancelled

This commit is contained in:
WarmUpTill 2026-06-01 20:13:00 +02:00 committed by WarmUpTill
parent 1ce332a91d
commit ed17c2ac6d
5 changed files with 47 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#include "twitch-helpers.hpp"
#include <log-helper.hpp>
#include <plugin-state-helpers.hpp>
#undef DispatchMessage
@ -339,6 +340,24 @@ static constexpr std::string_view defaultURL =
std::map<TwitchChatConnection::ChatMapKey, std::weak_ptr<TwitchChatConnection>>
TwitchChatConnection::_chatMap = {};
void TwitchChatConnection::DisconnectAll()
{
for (auto &[key, weakConn] : _chatMap) {
auto conn = weakConn.lock();
if (conn) {
conn->Disconnect();
}
}
}
static bool setupChatConnectionSupport()
{
AddStopStep(TwitchChatConnection::DisconnectAll);
return true;
}
static bool _chatSetup = setupChatConnectionSupport();
TwitchChatConnection::TwitchChatConnection(const TwitchToken &token,
const TwitchChannel &channel)
: QObject(nullptr),

View File

@ -84,6 +84,7 @@ public:
static std::shared_ptr<TwitchChatConnection>
GetChatConnection(const TwitchToken &token,
const TwitchChannel &channel);
static void DisconnectAll();
[[nodiscard]] ChatMessageBuffer RegisterForMessages();
[[nodiscard]] ChatMessageBuffer RegisterForWhispers();
void SendChatMessage(const std::string &message);

View File

@ -3,6 +3,7 @@
#include "twitch-helpers.hpp"
#include <log-helper.hpp>
#include <plugin-state-helpers.hpp>
#ifdef VERIFY_TIMESTAMPS
#include "date/tz.h"
@ -71,6 +72,22 @@ void EventSub::UnregisterInstance()
_instances.erase(it, _instances.end());
}
void EventSub::DisconnectAll()
{
std::lock_guard<std::mutex> lock(_instancesMtx);
for (auto *instance : _instances) {
instance->Disconnect();
}
}
static bool setupEventSubSupport()
{
AddStopStep(EventSub::DisconnectAll);
return true;
}
static bool _eventSubSetup = setupEventSubSupport();
void EventSub::ConnectThread()
{
_client->reset();

View File

@ -53,6 +53,7 @@ public:
void Connect();
void Disconnect();
static void DisconnectAll();
[[nodiscard]] EventSubMessageBuffer RegisterForEvents();
bool SubscriptionIsActive(const std::string &id);
static std::string AddEventSubscription(std::shared_ptr<TwitchToken>,

View File

@ -547,16 +547,18 @@ bool MacroConditionTwitch::CheckChatMessageRemove(TwitchToken &token)
bool MacroConditionTwitch::ChatConnectionIsSetup(TwitchToken &token)
{
if (_chatConnection) {
_chatConnection->ConnectToChat();
return true;
}
_chatConnection =
TwitchChatConnection::GetChatConnection(token, _channel);
if (!_chatConnection) {
_chatConnection = TwitchChatConnection::GetChatConnection(
token, _channel);
if (!_chatConnection) {
return false;
}
_chatBuffer = _chatConnection->RegisterForMessages();
return false;
}
return true;
_chatBuffer = _chatConnection->RegisterForMessages();
return false;
}
bool MacroConditionTwitch::HandleChatEvents(