2
0

websocketserver.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #if RTC_ENABLE_WEBSOCKET
  10. #include <atomic>
  11. #include <chrono>
  12. #include <iostream>
  13. #include <memory>
  14. #include <thread>
  15. using namespace rtc;
  16. using namespace std;
  17. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  18. void test_websocketserver() {
  19. InitLogger(LogLevel::Debug);
  20. const string myMessage = "Hello world from client";
  21. WebSocketServer::Configuration serverConfig;
  22. serverConfig.port = 48080;
  23. serverConfig.enableTls = true;
  24. // serverConfig.certificatePemFile = ...
  25. // serverConfig.keyPemFile = ...
  26. WebSocketServer server(std::move(serverConfig));
  27. shared_ptr<WebSocket> client;
  28. server.onClient([&client](shared_ptr<WebSocket> incoming) {
  29. cout << "WebSocketServer: Client connection received" << endl;
  30. client = incoming;
  31. if(auto addr = client->remoteAddress())
  32. cout << "WebSocketServer: Client remote address is " << *addr << endl;
  33. client->onOpen([wclient = make_weak_ptr(client)]() {
  34. cout << "WebSocketServer: Client connection open" << endl;
  35. if(auto client = wclient.lock())
  36. if(auto path = client->path())
  37. cout << "WebSocketServer: Requested path is " << *path << endl;
  38. });
  39. client->onClosed([]() {
  40. cout << "WebSocketServer: Client connection closed" << endl;
  41. });
  42. client->onMessage([wclient = make_weak_ptr(client)](variant<binary, string> message) {
  43. if(auto client = wclient.lock())
  44. client->send(std::move(message));
  45. });
  46. });
  47. WebSocket::Configuration config;
  48. config.disableTlsVerification = true;
  49. WebSocket ws(std::move(config));
  50. ws.onOpen([&ws, &myMessage]() {
  51. cout << "WebSocket: Open" << endl;
  52. ws.send(myMessage);
  53. });
  54. ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
  55. std::atomic<bool> received = false;
  56. ws.onMessage([&received, &myMessage](variant<binary, string> message) {
  57. if (holds_alternative<string>(message)) {
  58. string str = std::move(get<string>(message));
  59. if ((received = (str == myMessage)))
  60. cout << "WebSocket: Received expected message" << endl;
  61. else
  62. cout << "WebSocket: Received UNEXPECTED message" << endl;
  63. }
  64. });
  65. ws.open("wss://localhost:48080/");
  66. int attempts = 15;
  67. while ((!ws.isOpen() || !received) && attempts--)
  68. this_thread::sleep_for(1s);
  69. if (!ws.isOpen())
  70. throw runtime_error("WebSocket is not open");
  71. if (!received)
  72. throw runtime_error("Expected message not received");
  73. ws.close();
  74. this_thread::sleep_for(1s);
  75. server.stop();
  76. this_thread::sleep_for(1s);
  77. cout << "Success" << endl;
  78. }
  79. #endif