miggor-StreamGraph/addons/no_twitch/websocket_client.gd
Lera Elvoé b55a462945 Add OBS and Twitch nodes. Improve UX significantly. Rework groups from the ground up with a new instancing feature. Open to the public. (#18)
After months of work and over a hundred commits on this repo alone (not to mention the old, half-working repos on GitHub), StreamGraph is finally ready to be shown to the public, even if in an incomplete state.

This PR is a culmination of numerous design discussions, re-writes, and hours spent by both @Eroax and myself.

Reviewed-on: https://codeberg.org/Eroax/StreamGraph/pulls/18
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
2023-12-15 21:44:25 +00:00

58 lines
1.4 KiB
GDScript

extends WebSocketPeer
class_name Websocket_Client
## Helper Class for handling the freaking polling of a WebSocketPeer
## Emitted when [method WebSocketPeer.get_ready_state()] returns
## [member WebSocketPeer.STATE_OPEN]
signal socket_open
## Emitted when [method WebSocketPeer.get_ready_state()] returns
## [member WebSocketPeer.STATE_CLOSED]
signal socket_closed(close_code, close_reason)
## Emitted when [method WebSocketPeer.get_ready_state()] returns
## [member WebSocketPeer.STATE_CONNECTING]
signal socket_connecting
## Emitted when [method WebSocketPeer.get_ready_state()] returns
## [member WebSocketPeer.STATE_CLOSING]
signal socket_closing
## Emitted when [method WebSocketPeer.get_available_packets()] returns greater
## than 0. Or, when a packet has been received.
signal packet_received(packet_data)
## Works as a wrapper around [method WebSocketPeer.poll] to handle the logic of
## checking get_ready_state() more simply.
func poll_socket():
poll()
var state = get_ready_state()
match state:
STATE_OPEN:
socket_open.emit(get_connected_host(), get_connected_port())
if get_available_packet_count() > 0:
packet_received.emit(get_packet())
STATE_CONNECTING:
socket_connecting.emit()
STATE_CLOSING:
socket_closing.emit()
STATE_CLOSED:
socket_closed.emit(get_close_code(), get_close_reason())