Advertisement
SKaminari

main.gd

Oct 25th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Control
  2.  
  3. const NetworkClient = preload("res://scripts/websockets_client.gd")
  4. const Packet = preload("res://scripts/packet.gd")
  5.  
  6.  
  7. @onready var _network_client = NetworkClient.new()
  8.  
  9. var state: Callable
  10.  
  11. func _ready() -> void:
  12.     _network_client.connected.connect(self._handle_client_connected)
  13.     _network_client.disconnected.connect(self._handle_client_disconnected)
  14.     _network_client.error.connect(self._handle_network_error)
  15.     _network_client.data.connect(self._handle_network_data)
  16.  
  17.     add_child(_network_client)
  18.     _network_client.connect_to_server("127.0.0.1", 8081)
  19.  
  20. func _handle_client_connected():
  21.     print("Client connected to server!")
  22.  
  23. func _handle_client_disconnected(was_clean: bool) -> void:
  24.     OS.alert("Disconnected from server%s" % ["." if was_clean else " unexpectedly"])
  25.     get_tree().quit()
  26.  
  27. func _handle_network_data(data: String) -> void:
  28.     print("Received server data: ", data)
  29.     var action_payloads: Array = Packet.json_to_action_payloads(data)
  30.     var p: Packet = Packet.new(action_payloads[0], action_payloads[1])
  31.     state.call(p)
  32.  
  33. func _handle_network_error():
  34.     OS.alert("There was an error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement