Added Keepalive + Reconnects to NoTwitch Eventsub_Socket

This commit is contained in:
Eroax 2024-02-09 02:04:58 -07:00
parent 5e81a392cd
commit 5ab9021800

View file

@ -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):