websocket_client.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*************************************************************************/
  2. /* websocket_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. int p_len = -1;
  42. int port = 80;
  43. bool ssl = false;
  44. if (host.begins_with("wss://")) {
  45. ssl = true; // we should implement this
  46. host = host.substr(6, host.length() - 6);
  47. port = 443;
  48. } else {
  49. ssl = false;
  50. if (host.begins_with("ws://")) {
  51. host = host.substr(5, host.length() - 5);
  52. }
  53. }
  54. // Path
  55. p_len = host.find("/");
  56. if (p_len != -1) {
  57. path = host.substr(p_len, host.length() - p_len);
  58. host = host.substr(0, p_len);
  59. }
  60. // Port
  61. p_len = host.rfind(":");
  62. if (p_len != -1 && p_len == host.find(":")) {
  63. port = host.substr(p_len, host.length() - p_len).to_int();
  64. host = host.substr(0, p_len);
  65. }
  66. return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
  67. }
  68. void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) {
  69. verify_ssl = p_verify_ssl;
  70. }
  71. bool WebSocketClient::is_verify_ssl_enabled() const {
  72. return verify_ssl;
  73. }
  74. Ref<X509Certificate> WebSocketClient::get_trusted_ssl_certificate() const {
  75. return ssl_cert;
  76. }
  77. void WebSocketClient::set_trusted_ssl_certificate(Ref<X509Certificate> p_cert) {
  78. ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED);
  79. ssl_cert = p_cert;
  80. }
  81. bool WebSocketClient::is_server() const {
  82. return false;
  83. }
  84. void WebSocketClient::_on_peer_packet() {
  85. if (_is_multiplayer) {
  86. _process_multiplayer(get_peer(1), 1);
  87. } else {
  88. emit_signal("data_received");
  89. }
  90. }
  91. void WebSocketClient::_on_connect(String p_protocol) {
  92. if (_is_multiplayer) {
  93. // need to wait for ID confirmation...
  94. } else {
  95. emit_signal("connection_established", p_protocol);
  96. }
  97. }
  98. void WebSocketClient::_on_close_request(int p_code, String p_reason) {
  99. emit_signal("server_close_request", p_code, p_reason);
  100. }
  101. void WebSocketClient::_on_disconnect(bool p_was_clean) {
  102. if (_is_multiplayer) {
  103. emit_signal("connection_failed");
  104. } else {
  105. emit_signal("connection_closed", p_was_clean);
  106. }
  107. }
  108. void WebSocketClient::_on_error() {
  109. if (_is_multiplayer) {
  110. emit_signal("connection_failed");
  111. } else {
  112. emit_signal("connection_error");
  113. }
  114. }
  115. void WebSocketClient::_bind_methods() {
  116. 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>()));
  117. ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
  118. ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host);
  119. ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port);
  120. ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
  121. ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
  122. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
  123. ClassDB::bind_method(D_METHOD("get_trusted_ssl_certificate"), &WebSocketClient::get_trusted_ssl_certificate);
  124. ClassDB::bind_method(D_METHOD("set_trusted_ssl_certificate"), &WebSocketClient::set_trusted_ssl_certificate);
  125. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
  126. ADD_SIGNAL(MethodInfo("data_received"));
  127. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  128. ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
  129. ADD_SIGNAL(MethodInfo("connection_closed", PropertyInfo(Variant::BOOL, "was_clean_close")));
  130. ADD_SIGNAL(MethodInfo("connection_error"));
  131. }