From 5ab9021800f38e46a5cf0bed0ae465cdec819048 Mon Sep 17 00:00:00 2001 From: Eroax Date: Fri, 9 Feb 2024 02:04:58 -0700 Subject: [PATCH] Added Keepalive + Reconnects to NoTwitch Eventsub_Socket --- addons/no_twitch/eventsub_socket.gd | 44 +++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/addons/no_twitch/eventsub_socket.gd b/addons/no_twitch/eventsub_socket.gd index 993766c..d2a9c5f 100644 --- a/addons/no_twitch/eventsub_socket.gd +++ b/addons/no_twitch/eventsub_socket.gd @@ -11,23 +11,47 @@ signal notif_received(data) signal welcome_received() +var keepalive_timer := 0 +var timeout_time : int -func _init(owner): +var subscribed_events : Array[Twitch_Connection.EventSub_Subscription] + + +func _init(owner, timeout : int): connection = owner + timeout_time = timeout packet_received.connect(data_received) -func connect_to_eventsub(events : Array[Twitch_Connection.EventSub_Subscription] = []): +## Overrides the default poll function for [Websocket_Client] to add functionality for a keepalive timer and reconnecting when the connection is lost. +func poll_socket(): + + super() + + keepalive_timer += connection.get_process_delta_time() + if keepalive_timer >= timeout_time: + + socket_closed.emit() + close() + connect_to_eventsub(subscribed_events) + + + +## Handles setting up the connection to EventSub with an Array of the Events that should be subscribed to. +func connect_to_eventsub(events : Array[Twitch_Connection.EventSub_Subscription]): connect_to_url(eventsub_url) await welcome_received - if events.is_empty(): - - return - + subscribed_events = events + + return await subscribe_to_events(events) + + +## Utility function for subscribing to multiple Twitch EventSub events at once. +func subscribe_to_events(events : Array[Twitch_Connection.EventSub_Subscription]): var responses : Array[Twitch_Connection.HTTPResponse] @@ -58,6 +82,7 @@ func data_received(packet : PackedByteArray): "session_ping": + print("Ping Received") send_pong(info) @@ -66,6 +91,13 @@ func data_received(packet : PackedByteArray): notif_received.emit(info) + "session_keepalive": + + keepalive_timer = 0 + + + + print(info) func send_pong(pong):