2024-01-25 07:36:05 +01:00
|
|
|
extends Websocket_Client
|
|
|
|
|
|
|
|
const eventsub_url := "wss://eventsub.wss.twitch.tv/ws"
|
|
|
|
|
|
|
|
## Stores the "session id" for this EventSub connection.
|
|
|
|
var session_id
|
|
|
|
|
2024-03-17 11:28:54 +01:00
|
|
|
var connection : No_Twitch
|
2024-01-25 07:36:05 +01:00
|
|
|
|
|
|
|
signal notif_received(data)
|
|
|
|
|
|
|
|
signal welcome_received()
|
|
|
|
|
2024-01-26 11:26:22 +01:00
|
|
|
var keepalive_timer := 0
|
|
|
|
var timeout_time : int
|
|
|
|
|
2024-03-17 11:28:54 +01:00
|
|
|
## Dictionary used for storing all the subscribed events in the format "subscription_type : No_Twitch.EventSub_Subscription"
|
2024-01-26 11:26:22 +01:00
|
|
|
var subscribed_events : Dictionary
|
|
|
|
|
|
|
|
|
|
|
|
func _init(owner, timeout : int):
|
2024-01-25 07:36:05 +01:00
|
|
|
|
|
|
|
connection = owner
|
2024-01-26 11:26:22 +01:00
|
|
|
timeout_time = timeout
|
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
packet_received.connect(data_received)
|
|
|
|
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
## 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.values())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Handles setting up the connection to EventSub with an Array of the Events that should be subscribed to.
|
2024-03-17 11:28:54 +01:00
|
|
|
func connect_to_eventsub(events : Array[No_Twitch.EventSub_Subscription]):
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
connect_to_url(eventsub_url)
|
|
|
|
await welcome_received
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
for all in events:
|
|
|
|
|
|
|
|
subscribed_events[all.subscription_type] = all
|
|
|
|
|
|
|
|
|
|
|
|
return await subscribe_to_events(events)
|
|
|
|
|
|
|
|
|
|
|
|
## Utility function for subscribing to multiple Twitch EventSub events at once.
|
2024-03-17 11:28:54 +01:00
|
|
|
func subscribe_to_events(events : Array[No_Twitch.EventSub_Subscription]):
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-03-17 11:28:54 +01:00
|
|
|
var responses : Array[No_Twitch.HTTPResponse]
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
for all in events:
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
responses.append(await connection.add_eventsub_subscription(all))
|
|
|
|
|
|
|
|
|
|
|
|
if responses.size() == 1:
|
|
|
|
|
|
|
|
return responses[0]
|
|
|
|
|
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
return responses
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
|
2024-03-17 11:28:54 +01:00
|
|
|
func new_eventsub_subscription(info, event_subscription : No_Twitch.EventSub_Subscription):
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
if !event_subscription in subscribed_events:
|
|
|
|
|
|
|
|
subscribed_events[event_subscription.subscription_type] = event_subscription
|
|
|
|
|
|
|
|
event_subscription.sub_id = info.data.id
|
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
|
|
|
|
func data_received(packet : PackedByteArray):
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
var info = JSON.parse_string(packet.get_string_from_utf8())
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
match info.metadata.message_type:
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
"session_welcome":
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
session_id = info.payload.session.id
|
|
|
|
welcome_received.emit()
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
"session_ping":
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
send_pong(info)
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
"notification":
|
2024-01-26 11:26:22 +01:00
|
|
|
|
2024-01-25 07:36:05 +01:00
|
|
|
notif_received.emit(info)
|
2024-01-26 11:26:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
"session_keepalive":
|
|
|
|
|
|
|
|
keepalive_timer = 0
|
|
|
|
|
|
|
|
print(info)
|
|
|
|
|
2024-02-11 20:07:58 +01:00
|
|
|
|
|
|
|
func send_pong(pong):
|
|
|
|
|
|
|
|
pong.metadata.message_type = "session_pong"
|
|
|
|
send_text(str(pong))
|