miggor-StreamGraph/addons/no_twitch/eventsub_socket.gd
2024-01-26 03:29:23 -07:00

75 lines
1.2 KiB
GDScript

extends Websocket_Client
const eventsub_url := "wss://eventsub.wss.twitch.tv/ws"
## Stores the "session id" for this EventSub connection.
var session_id
var connection : Twitch_Connection
signal notif_received(data)
signal welcome_received()
func _init(owner):
connection = owner
packet_received.connect(data_received)
func connect_to_eventsub(events : Array[Twitch_Connection.EventSub_Subscription] = []):
connect_to_url(eventsub_url)
await welcome_received
if events.is_empty():
return
var responses : Array[Twitch_Connection.HTTPResponse]
for all in events:
responses.append(await connection.add_eventsub_subscription(all))
if responses.size() == 1:
return responses[0]
return responses
func data_received(packet : PackedByteArray):
var info = JSON.parse_string(packet.get_string_from_utf8())
match info.metadata.message_type:
"session_welcome":
session_id = info.payload.session.id
welcome_received.emit()
"session_ping":
send_pong(info)
"notification":
notif_received.emit(info)
func send_pong(pong):
pong.metadata.message_type = "session_pong"
send_text(str(pong))