client.gd 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. extends Control
  2. @onready var _client: WebSocketClient = $WebSocketClient
  3. @onready var _log_dest: RichTextLabel = $Panel/VBoxContainer/RichTextLabel
  4. @onready var _line_edit: LineEdit = $Panel/VBoxContainer/Send/LineEdit
  5. @onready var _host: LineEdit = $Panel/VBoxContainer/Connect/Host
  6. func info(msg: String) -> void:
  7. print(msg)
  8. _log_dest.add_text(str(msg) + "\n")
  9. #region Client signals
  10. func _on_web_socket_client_connection_closed() -> void:
  11. var ws := _client.get_socket()
  12. info("Client just disconnected with code: %s, reson: %s" % [ws.get_close_code(), ws.get_close_reason()])
  13. func _on_web_socket_client_connected_to_server() -> void:
  14. info("Client just connected with protocol: %s" % _client.get_socket().get_selected_protocol())
  15. func _on_web_socket_client_message_received(message: String) -> void:
  16. info("%s" % message)
  17. #endregion
  18. #region UI signals
  19. func _on_send_pressed() -> void:
  20. if _line_edit.text.is_empty():
  21. return
  22. info("Sending message: %s" % [_line_edit.text])
  23. _client.send(_line_edit.text)
  24. _line_edit.text = ""
  25. func _on_connect_toggled(pressed: bool) -> void:
  26. if not pressed:
  27. _client.close()
  28. return
  29. if _host.text.is_empty():
  30. return
  31. info("Connecting to host: %s." % [_host.text])
  32. var err := _client.connect_to_url(_host.text)
  33. if err != OK:
  34. info("Error connecting to host: %s" % [_host.text])
  35. return
  36. #endregion