miggor-StreamGraph/addons/no_twitch/eventsub_socket.gd
Lera Elvoé d0313b9697 Twitch updates pt. 1 (#56)
- Add credential saving for Twitch
- Sync NoTwitch with upstream to allow for API and EventSub access
- Add Twitch Connected Account Info and Twitch Request User Info nodes

Co-authored-by: Eroax <eroaxe.business@gmail.com>
Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/56
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
2024-01-25 06:36:05 +00:00

70 lines
1.1 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(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))