websocketserver.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright (c) 2021 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "rtc/rtc.hpp"
  19. #if RTC_ENABLE_WEBSOCKET
  20. #include <atomic>
  21. #include <chrono>
  22. #include <iostream>
  23. #include <memory>
  24. #include <thread>
  25. using namespace rtc;
  26. using namespace std;
  27. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  28. void test_websocketserver() {
  29. InitLogger(LogLevel::Debug);
  30. const string myMessage = "Hello world from client";
  31. WebSocketServer::Configuration serverConfig;
  32. serverConfig.port = 48080;
  33. WebSocketServer server(std::move(serverConfig));
  34. shared_ptr<WebSocket> client;
  35. server.onClient([&client](shared_ptr<WebSocket> incoming) {
  36. cout << "WebSocketServer: Client connection received" << endl;
  37. client = incoming;
  38. if(auto addr = client->remoteAddress())
  39. cout << "WebSocketServer: Client remote address is " << *addr << endl;
  40. client->onOpen([wclient = make_weak_ptr(client)]() {
  41. cout << "WebSocketServer: Client connection open" << endl;
  42. if(auto client = wclient.lock())
  43. if(auto path = client->path())
  44. cout << "WebSocketServer: Requested path is " << *path << endl;
  45. });
  46. client->onClosed([]() {
  47. cout << "WebSocketServer: Client connection closed" << endl;
  48. });
  49. client->onMessage([wclient = make_weak_ptr(client)](variant<binary, string> message) {
  50. if(auto client = wclient.lock())
  51. client->send(std::move(message));
  52. });
  53. });
  54. WebSocket::Configuration config;
  55. config.disableTlsVerification = true;
  56. WebSocket ws(std::move(config));
  57. ws.onOpen([&ws, &myMessage]() {
  58. cout << "WebSocket: Open" << endl;
  59. ws.send(myMessage);
  60. });
  61. ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
  62. std::atomic<bool> received = false;
  63. ws.onMessage([&received, &myMessage](variant<binary, string> message) {
  64. if (holds_alternative<string>(message)) {
  65. string str = std::move(get<string>(message));
  66. if ((received = (str == myMessage)))
  67. cout << "WebSocket: Received expected message" << endl;
  68. else
  69. cout << "WebSocket: Received UNEXPECTED message" << endl;
  70. }
  71. });
  72. ws.open("ws://localhost:48080/");
  73. int attempts = 10;
  74. while ((!ws.isOpen() || !received) && attempts--)
  75. this_thread::sleep_for(1s);
  76. if (!ws.isOpen())
  77. throw runtime_error("WebSocket is not open");
  78. if (!received)
  79. throw runtime_error("Expected message not received");
  80. ws.close();
  81. this_thread::sleep_for(1s);
  82. server.stop();
  83. this_thread::sleep_for(1s);
  84. cout << "Success" << endl;
  85. }
  86. #endif