client.gd 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. extends Control
  2. @onready var _client: WebSocketClient = $WebSocketClient
  3. @onready var _log_dest = $Panel/VBoxContainer/RichTextLabel
  4. @onready var _line_edit = $Panel/VBoxContainer/Send/LineEdit
  5. @onready var _host = $Panel/VBoxContainer/Connect/Host
  6. func info(msg):
  7. print(msg)
  8. _log_dest.add_text(str(msg) + "\n")
  9. # Client signals
  10. func _on_web_socket_client_connection_closed():
  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():
  14. info("Client just connected with protocol: %s" % _client.get_socket().get_selected_protocol())
  15. func _on_web_socket_client_message_received(message):
  16. info("%s" % message)
  17. # UI signals.
  18. func _on_send_pressed():
  19. if _line_edit.text == "":
  20. return
  21. info("Sending message: %s" % [_line_edit.text])
  22. _client.send(_line_edit.text)
  23. _line_edit.text = ""
  24. func _on_connect_toggled(pressed):
  25. if not pressed:
  26. _client.close()
  27. return
  28. if _host.text == "":
  29. return
  30. info("Connecting to host: %s." % [_host.text])
  31. var err = _client.connect_to_url(_host.text)
  32. if err != OK:
  33. info("Error connecting to host: %s" % [_host.text])
  34. return