Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Control
- const NetworkClient = preload("res://scripts/websockets_client.gd")
- const Packet = preload("res://scripts/packet.gd")
- @onready var _network_client = NetworkClient.new()
- var state: Callable
- func _ready() -> void:
- _network_client.connected.connect(self._handle_client_connected)
- _network_client.disconnected.connect(self._handle_client_disconnected)
- _network_client.error.connect(self._handle_network_error)
- _network_client.data.connect(self._handle_network_data)
- add_child(_network_client)
- _network_client.connect_to_server("127.0.0.1", 8081)
- func _handle_client_connected():
- print("Client connected to server!")
- func _handle_client_disconnected(was_clean: bool) -> void:
- OS.alert("Disconnected from server%s" % ["." if was_clean else " unexpectedly"])
- get_tree().quit()
- func _handle_network_data(data: String) -> void:
- print("Received server data: ", data)
- var action_payloads: Array = Packet.json_to_action_payloads(data)
- var p: Packet = Packet.new(action_payloads[0], action_payloads[1])
- state.call(p)
- func _handle_network_error():
- OS.alert("There was an error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement