miggor-StreamGraph/addons/no_twitch/eventsub_socket.gd

71 lines
1.1 KiB
GDScript3
Raw Normal View History

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(connection.add_eventsub_subscription(all))
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))