websocket_client.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // Path
  54. p_len = host.find("/");
  55. if (p_len != -1) {
  56. path = host.substr(p_len, host.length() - p_len);
  57. host = host.substr(0, p_len);
  58. }
  59. // Port
  60. p_len = host.find_last(":");
  61. if (p_len != -1 && p_len == host.find(":")) {
  62. port = host.substr(p_len, host.length() - p_len).to_int();
  63. host = host.substr(0, p_len);
  64. }
  65. return connect_to_host(host, path, port, ssl, p_protocols, p_custom_headers);
  66. }
  67. void WebSocketClient::set_verify_ssl_enabled(bool p_verify_ssl) {
  68. verify_ssl = p_verify_ssl;
  69. }
  70. bool WebSocketClient::is_verify_ssl_enabled() const {
  71. return verify_ssl;
  72. }
  73. Ref<X509Certificate> WebSocketClient::get_trusted_ssl_certificate() const {
  74. return ssl_cert;
  75. }
  76. void WebSocketClient::set_trusted_ssl_certificate(Ref<X509Certificate> p_cert) {
  77. ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED);
  78. ssl_cert = p_cert;
  79. }
  80. bool WebSocketClient::is_server() const {
  81. return false;
  82. }
  83. void WebSocketClient::_on_peer_packet() {
  84. if (_is_multiplayer) {
  85. _process_multiplayer(get_peer(1), 1);
  86. } else {
  87. emit_signal("data_received");
  88. }
  89. }
  90. void WebSocketClient::_on_connect(String p_protocol) {
  91. if (_is_multiplayer) {
  92. // need to wait for ID confirmation...
  93. } else {
  94. emit_signal("connection_established", p_protocol);
  95. }
  96. }
  97. void WebSocketClient::_on_close_request(int p_code, String p_reason) {
  98. emit_signal("server_close_request", p_code, p_reason);
  99. }
  100. void WebSocketClient::_on_disconnect(bool p_was_clean) {
  101. if (_is_multiplayer) {
  102. emit_signal("connection_failed");
  103. } else {
  104. emit_signal("connection_closed", p_was_clean);
  105. }
  106. }
  107. void WebSocketClient::_on_error() {
  108. if (_is_multiplayer) {
  109. emit_signal("connection_failed");
  110. } else {
  111. emit_signal("connection_error");
  112. }
  113. }
  114. void WebSocketClient::_bind_methods() {
  115. 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>()));
  116. ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
  117. ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host);
  118. ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port);
  119. ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
  120. ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
  122. ClassDB::bind_method(D_METHOD("get_trusted_ssl_certificate"), &WebSocketClient::get_trusted_ssl_certificate);
  123. ClassDB::bind_method(D_METHOD("set_trusted_ssl_certificate"), &WebSocketClient::set_trusted_ssl_certificate);
  124. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_ssl_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", 0), "set_trusted_ssl_certificate", "get_trusted_ssl_certificate");
  125. ADD_SIGNAL(MethodInfo("data_received"));
  126. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  127. ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
  128. ADD_SIGNAL(MethodInfo("connection_closed", PropertyInfo(Variant::BOOL, "was_clean_close")));
  129. ADD_SIGNAL(MethodInfo("connection_error"));
  130. }