websocket_client.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* websocket_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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, PoolVector<String> p_protocols, bool gd_mp_api) {
  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);
  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. bool WebSocketClient::is_server() const {
  74. return false;
  75. }
  76. void WebSocketClient::_on_peer_packet() {
  77. if (_is_multiplayer) {
  78. _process_multiplayer(get_peer(1), 1);
  79. } else {
  80. emit_signal("data_received");
  81. }
  82. }
  83. void WebSocketClient::_on_connect(String p_protocol) {
  84. if (_is_multiplayer) {
  85. // need to wait for ID confirmation...
  86. } else {
  87. emit_signal("connection_established", p_protocol);
  88. }
  89. }
  90. void WebSocketClient::_on_disconnect() {
  91. if (_is_multiplayer) {
  92. emit_signal("connection_failed");
  93. } else {
  94. emit_signal("connection_closed");
  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"), &WebSocketClient::connect_to_url, DEFVAL(PoolVector<String>()), DEFVAL(false));
  106. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &WebSocketClient::disconnect_from_host);
  107. ClassDB::bind_method(D_METHOD("set_verify_ssl_enabled", "enabled"), &WebSocketClient::set_verify_ssl_enabled);
  108. ClassDB::bind_method(D_METHOD("is_verify_ssl_enabled"), &WebSocketClient::is_verify_ssl_enabled);
  109. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "verify_ssl", PROPERTY_HINT_NONE, "", 0), "set_verify_ssl_enabled", "is_verify_ssl_enabled");
  110. ADD_SIGNAL(MethodInfo("data_received"));
  111. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  112. ADD_SIGNAL(MethodInfo("connection_closed"));
  113. ADD_SIGNAL(MethodInfo("connection_error"));
  114. }