websocketserver.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. serverConfig.enableTls = true;
  34. // serverConfig.certificatePemFile = ...
  35. // serverConfig.keyPemFile = ...
  36. WebSocketServer server(std::move(serverConfig));
  37. shared_ptr<WebSocket> client;
  38. server.onClient([&client](shared_ptr<WebSocket> incoming) {
  39. cout << "WebSocketServer: Client connection received" << endl;
  40. client = incoming;
  41. if(auto addr = client->remoteAddress())
  42. cout << "WebSocketServer: Client remote address is " << *addr << endl;
  43. client->onOpen([wclient = make_weak_ptr(client)]() {
  44. cout << "WebSocketServer: Client connection open" << endl;
  45. if(auto client = wclient.lock())
  46. if(auto path = client->path())
  47. cout << "WebSocketServer: Requested path is " << *path << endl;
  48. });
  49. client->onClosed([]() {
  50. cout << "WebSocketServer: Client connection closed" << endl;
  51. });
  52. client->onMessage([wclient = make_weak_ptr(client)](variant<binary, string> message) {
  53. if(auto client = wclient.lock())
  54. client->send(std::move(message));
  55. });
  56. });
  57. WebSocket::Configuration config;
  58. config.disableTlsVerification = true;
  59. WebSocket ws(std::move(config));
  60. ws.onOpen([&ws, &myMessage]() {
  61. cout << "WebSocket: Open" << endl;
  62. ws.send(myMessage);
  63. });
  64. ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
  65. std::atomic<bool> received = false;
  66. ws.onMessage([&received, &myMessage](variant<binary, string> message) {
  67. if (holds_alternative<string>(message)) {
  68. string str = std::move(get<string>(message));
  69. if ((received = (str == myMessage)))
  70. cout << "WebSocket: Received expected message" << endl;
  71. else
  72. cout << "WebSocket: Received UNEXPECTED message" << endl;
  73. }
  74. });
  75. ws.open("wss://localhost:48080/");
  76. int attempts = 15;
  77. while ((!ws.isOpen() || !received) && attempts--)
  78. this_thread::sleep_for(1s);
  79. if (!ws.isOpen())
  80. throw runtime_error("WebSocket is not open");
  81. if (!received)
  82. throw runtime_error("Expected message not received");
  83. ws.close();
  84. this_thread::sleep_for(1s);
  85. server.stop();
  86. this_thread::sleep_for(1s);
  87. cout << "Success" << endl;
  88. }
  89. #endif