websocket_client.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }
  34. WebSocketClient::~WebSocketClient() {
  35. }
  36. Error WebSocketClient::connect_to_url(String p_url, PoolVector<String> p_protocols, bool gd_mp_api) {
  37. _is_multiplayer = gd_mp_api;
  38. String host = p_url;
  39. String path = "/";
  40. int p_len = -1;
  41. int port = 80;
  42. bool ssl = false;
  43. if (host.begins_with("wss://")) {
  44. ssl = true; // we should implement this
  45. host = host.substr(6, host.length() - 6);
  46. port = 443;
  47. } else {
  48. ssl = false;
  49. if (host.begins_with("ws://"))
  50. host = host.substr(5, host.length() - 5);
  51. }
  52. // Path
  53. p_len = host.find("/");
  54. if (p_len != -1) {
  55. path = host.substr(p_len, host.length() - p_len);
  56. host = host.substr(0, p_len);
  57. }
  58. // Port
  59. p_len = host.find_last(":");
  60. if (p_len != -1 && p_len == host.find(":")) {
  61. port = host.substr(p_len, host.length() - p_len).to_int();
  62. host = host.substr(0, p_len);
  63. }
  64. return connect_to_host(host, path, port, ssl, p_protocols);
  65. }
  66. bool WebSocketClient::is_server() const {
  67. return false;
  68. }
  69. void WebSocketClient::_on_peer_packet() {
  70. if (_is_multiplayer) {
  71. _process_multiplayer(get_peer(1), 1);
  72. } else {
  73. emit_signal("data_received");
  74. }
  75. }
  76. void WebSocketClient::_on_connect(String p_protocol) {
  77. if (_is_multiplayer) {
  78. // need to wait for ID confirmation...
  79. } else {
  80. emit_signal("connection_established", p_protocol);
  81. }
  82. }
  83. void WebSocketClient::_on_disconnect() {
  84. if (_is_multiplayer) {
  85. emit_signal("connection_failed");
  86. } else {
  87. emit_signal("connection_closed");
  88. }
  89. }
  90. void WebSocketClient::_on_error() {
  91. if (_is_multiplayer) {
  92. emit_signal("connection_failed");
  93. } else {
  94. emit_signal("connection_error");
  95. }
  96. }
  97. void WebSocketClient::_bind_methods() {
  98. ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api"), &WebSocketClient::connect_to_url, DEFVAL(PoolVector<String>()), DEFVAL(false));
  99. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &WebSocketClient::disconnect_from_host);
  100. ADD_SIGNAL(MethodInfo("data_received"));
  101. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  102. ADD_SIGNAL(MethodInfo("connection_closed"));
  103. ADD_SIGNAL(MethodInfo("connection_error"));
  104. }