websocket_client.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*************************************************************************/
  2. /* websocket_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "websocket_client.h"
  31. GDCINULL(WebSocketClient);
  32. WebSocketClient::WebSocketClient() {
  33. verify_ssl = true;
  34. }
  35. WebSocketClient::~WebSocketClient() {
  36. }
  37. Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_protocols, bool gd_mp_api, const Vector<String> p_custom_headers) {
  38. _is_multiplayer = gd_mp_api;
  39. String host = p_url;
  40. String path;
  41. String scheme;
  42. int port = 0;
  43. Error err = p_url.parse_url(scheme, host, port, path);
  44. ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
  45. bool ssl = false;
  46. if (scheme == "wss://") {
  47. ssl = true;
  48. }
  49. if (port == 0) {
  50. port = ssl ? 443 : 80;
  51. }
  52. if (path.empty()) {
  53. path = "/";
  54. }
  55. return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
  56. }
  57. void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) {
  58. verify_ssl = p_verify_ssl;
  59. }
  60. bool WebSocketClient::is_verify_ssl_enabled() const {
  61. return verify_ssl;
  62. }
  63. Ref<X509Certificate> WebSocketClient::get_trusted_ssl_certificate() const {
  64. return ssl_cert;
  65. }
  66. void WebSocketClient::set_trusted_ssl_certificate(Ref<X509Certificate> p_cert) {
  67. ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED);
  68. ssl_cert = p_cert;
  69. }
  70. bool WebSocketClient::is_server() const {
  71. return false;
  72. }
  73. void WebSocketClient::_on_peer_packet() {
  74. if (_is_multiplayer) {
  75. _process_multiplayer(get_peer(1), 1);
  76. } else {
  77. emit_signal("data_received");
  78. }
  79. }
  80. void WebSocketClient::_on_connect(String p_protocol) {
  81. if (_is_multiplayer) {
  82. // need to wait for ID confirmation...
  83. } else {
  84. emit_signal("connection_established", p_protocol);
  85. }
  86. }
  87. void WebSocketClient::_on_close_request(int p_code, String p_reason) {
  88. emit_signal("server_close_request", p_code, p_reason);
  89. }
  90. void WebSocketClient::_on_disconnect(bool p_was_clean) {
  91. if (_is_multiplayer) {
  92. emit_signal("connection_failed");
  93. } else {
  94. emit_signal("connection_closed", p_was_clean);
  95. }
  96. }
  97. void WebSocketClient::_on_error() {
  98. if (_is_multiplayer) {
  99. emit_signal("connection_failed");
  100. } else {
  101. emit_signal("connection_error");
  102. }
  103. }
  104. void WebSocketClient::_bind_methods() {
  105. ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api", "custom_headers"), &WebSocketClient::connect_to_url, DEFVAL(Vector<String>()), DEFVAL(false), DEFVAL(Vector<String>()));
  106. ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
  107. ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host);
  108. ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port);
  109. ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
  110. ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
  111. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
  112. ClassDB::bind_method(D_METHOD("get_trusted_ssl_certificate"), &WebSocketClient::get_trusted_ssl_certificate);
  113. ClassDB::bind_method(D_METHOD("set_trusted_ssl_certificate"), &WebSocketClient::set_trusted_ssl_certificate);
  114. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
  115. ADD_SIGNAL(MethodInfo("data_received"));
  116. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  117. ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
  118. ADD_SIGNAL(MethodInfo("connection_closed", PropertyInfo(Variant::BOOL, "was_clean_close")));
  119. ADD_SIGNAL(MethodInfo("connection_error"));
  120. }