websocket_server.cpp 3.9 KB

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