websocket_server.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*************************************************************************/
  2. /* websocket_server.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_server.h"
  30. GDCINULL(WebSocketServer);
  31. WebSocketServer::WebSocketServer() {
  32. _peer_id = 1;
  33. }
  34. WebSocketServer::~WebSocketServer() {
  35. }
  36. void WebSocketServer::_bind_methods() {
  37. ClassDB::bind_method(D_METHOD("is_listening"), &WebSocketServer::is_listening);
  38. ClassDB::bind_method(D_METHOD("listen", "port", "protocols", "gd_mp_api"), &WebSocketServer::listen, DEFVAL(PoolVector<String>()), DEFVAL(false));
  39. ClassDB::bind_method(D_METHOD("stop"), &WebSocketServer::stop);
  40. ClassDB::bind_method(D_METHOD("has_peer", "id"), &WebSocketServer::has_peer);
  41. ADD_SIGNAL(MethodInfo("client_disconnected", PropertyInfo(Variant::INT, "id")));
  42. ADD_SIGNAL(MethodInfo("client_connected", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "protocol")));
  43. ADD_SIGNAL(MethodInfo("data_received", PropertyInfo(Variant::INT, "id")));
  44. }
  45. NetworkedMultiplayerPeer::ConnectionStatus WebSocketServer::get_connection_status() const {
  46. if (is_listening())
  47. return CONNECTION_CONNECTED;
  48. return CONNECTION_DISCONNECTED;
  49. };
  50. bool WebSocketServer::is_server() const {
  51. return true;
  52. }
  53. void WebSocketServer::_on_peer_packet(int32_t p_peer_id) {
  54. if (_is_multiplayer) {
  55. _process_multiplayer(get_peer(p_peer_id), p_peer_id);
  56. } else {
  57. emit_signal("data_received", p_peer_id);
  58. }
  59. }
  60. void WebSocketServer::_on_connect(int32_t p_peer_id, String p_protocol) {
  61. if (_is_multiplayer) {
  62. // Send add to clients
  63. _send_add(p_peer_id);
  64. emit_signal("peer_connected", p_peer_id);
  65. } else {
  66. emit_signal("client_connected", p_peer_id, p_protocol);
  67. }
  68. }
  69. void WebSocketServer::_on_disconnect(int32_t p_peer_id) {
  70. if (_is_multiplayer) {
  71. // Send delete to clients
  72. _send_del(p_peer_id);
  73. emit_signal("peer_disconnected", p_peer_id);
  74. } else {
  75. emit_signal("client_disconnected", p_peer_id);
  76. }
  77. }