client.gd 694 B

1234567891011121314151617181920212223242526272829303132
  1. extends Node
  2. # The URL we will connect to.
  3. var websocket_url = "ws://localhost:9080"
  4. var socket := WebSocketPeer.new()
  5. func log_message(message):
  6. var time = "[color=#aaaaaa] %s [/color]" % Time.get_time_string_from_system()
  7. %TextClient.text += time + message + "\n"
  8. func _ready():
  9. if socket.connect_to_url(websocket_url) != OK:
  10. log_message("Unable to connect.")
  11. set_process(false)
  12. func _process(_delta):
  13. socket.poll()
  14. if socket.get_ready_state() == WebSocketPeer.STATE_OPEN:
  15. while socket.get_available_packet_count():
  16. log_message(socket.get_packet().get_string_from_ascii())
  17. func _exit_tree():
  18. socket.close()
  19. func _on_button_ping_pressed():
  20. socket.send_text("Ping")