websocket_client.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*************************************************************************/
  2. /* websocket_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT WEBSOCKET MODULE */
  6. /* https://github.com/LudiDorici/godot-websocket */
  7. /*************************************************************************/
  8. /* Copyright (c) 2017 Ludi Dorici, di Alessandrelli Fabio */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "websocket_client.h"
  30. GDCINULL(WebSocketClient);
  31. WebSocketClient::WebSocketClient() {
  32. }
  33. WebSocketClient::~WebSocketClient() {
  34. }
  35. Error WebSocketClient::connect_to_url(String p_url, PoolVector<String> p_protocols, bool gd_mp_api) {
  36. _is_multiplayer = gd_mp_api;
  37. String host = p_url;
  38. String path = "/";
  39. int p_len = -1;
  40. int port = 80;
  41. bool ssl = false;
  42. if (host.begins_with("wss://")) {
  43. ssl = true; // we should implement this
  44. host = host.substr(6, host.length() - 6);
  45. port = 443;
  46. } else {
  47. ssl = false;
  48. if (host.begins_with("ws://"))
  49. host = host.substr(5, host.length() - 5);
  50. }
  51. // Path
  52. p_len = host.find("/");
  53. if (p_len != -1) {
  54. path = host.substr(p_len, host.length() - p_len);
  55. host = host.substr(0, p_len);
  56. }
  57. // Port
  58. p_len = host.find_last(":");
  59. if (p_len != -1 && p_len == host.find(":")) {
  60. port = host.substr(p_len, host.length() - p_len).to_int();
  61. host = host.substr(0, p_len);
  62. }
  63. return connect_to_host(host, path, port, ssl, p_protocols);
  64. }
  65. bool WebSocketClient::is_server() const {
  66. return false;
  67. }
  68. void WebSocketClient::_on_peer_packet() {
  69. if (_is_multiplayer) {
  70. _process_multiplayer(get_peer(1), 1);
  71. } else {
  72. emit_signal("data_received");
  73. }
  74. }
  75. void WebSocketClient::_on_connect(String p_protocol) {
  76. if (_is_multiplayer) {
  77. // need to wait for ID confirmation...
  78. } else {
  79. emit_signal("connection_established", p_protocol);
  80. }
  81. }
  82. void WebSocketClient::_on_disconnect() {
  83. if (_is_multiplayer) {
  84. emit_signal("connection_failed");
  85. } else {
  86. emit_signal("connection_closed");
  87. }
  88. }
  89. void WebSocketClient::_on_error() {
  90. if (_is_multiplayer) {
  91. emit_signal("connection_failed");
  92. } else {
  93. emit_signal("connection_error");
  94. }
  95. }
  96. void WebSocketClient::_bind_methods() {
  97. ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api"), &WebSocketClient::connect_to_url, DEFVAL(PoolVector<String>()), DEFVAL(false));
  98. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &WebSocketClient::disconnect_from_host);
  99. ADD_SIGNAL(MethodInfo("data_received"));
  100. ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
  101. ADD_SIGNAL(MethodInfo("connection_closed"));
  102. ADD_SIGNAL(MethodInfo("connection_error"));
  103. }