fix(eventsub): check keepalive (#6058)

This commit is contained in:
nerix
2025-03-09 21:32:49 +01:00
committed by GitHub
parent 673ce8eecf
commit 9124c68278
8 changed files with 224 additions and 148 deletions
@@ -24,7 +24,8 @@ namespace chatterino::eventsub::lib::payload::session_welcome {
/// json_inner=session
struct Payload {
std::string id;
std::optional<std::string> reconnectURL;
std::optional<std::string> reconnectURL; // null in welcome message
std::optional<int> keepaliveTimeoutSeconds; // null in reconnect message
};
#include "twitch-eventsub-ws/payloads/session-welcome.inc"
@@ -7,21 +7,16 @@
#include <boost/beast/websocket/ssl.hpp>
#include <boost/json.hpp>
namespace chatterino::eventsub::lib::messages {
struct Metadata;
} // namespace chatterino::eventsub::lib::messages
namespace chatterino::eventsub::lib {
class Listener;
/**
* handleMessage takes the incoming message in the buffer, parses it
* as JSON then forwards it to the listener, if applicable.
*
* This is called from the Session, and is only provided if you are interested
* in building your own boost asio framework thing
**/
boost::system::error_code handleMessage(
std::unique_ptr<Listener> &listener,
const boost::beast::flat_buffer &buffer);
class Session : public std::enable_shared_from_this<Session>
{
public:
@@ -38,13 +33,17 @@ public:
Listener *getListener();
// public for testing
boost::system::error_code handleMessage(
const boost::beast::flat_buffer &buffer);
private:
void onResolve(boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results);
const boost::asio::ip::tcp::resolver::results_type &results);
void onConnect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type ep);
const boost::asio::ip::tcp::resolver::results_type::endpoint_type &ep);
void onSSLHandshake(boost::beast::error_code ec);
@@ -56,6 +55,14 @@ private:
void fail(boost::beast::error_code ec, std::string_view op);
boost::system::error_code onSessionWelcome(
const messages::Metadata &metadata, const boost::json::value &jv);
boost::system::error_code onSessionReconnect(const boost::json::value &jv);
boost::system::error_code onNotification(const messages::Metadata &metadata,
const boost::json::value &jv);
void checkKeepalive();
boost::asio::ip::tcp::resolver resolver;
boost::beast::websocket::stream<
boost::beast::ssl_stream<boost::beast::tcp_stream>>
@@ -66,6 +73,10 @@ private:
std::string path;
std::string userAgent;
std::unique_ptr<Listener> listener;
std::chrono::seconds keepaliveTimeout{0};
bool receivedMessage = false;
std::unique_ptr<boost::asio::system_timer> keepaliveTimer;
};
} // namespace chatterino::eventsub::lib