websocketserver.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "test.hpp"
  10. #if RTC_ENABLE_WEBSOCKET
  11. #include <atomic>
  12. #include <chrono>
  13. #include <iostream>
  14. #include <memory>
  15. #include <thread>
  16. using namespace rtc;
  17. using namespace std;
  18. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  19. TestResult test_websocketserver() {
  20. InitLogger(LogLevel::Debug);
  21. WebSocketServer::Configuration serverConfig;
  22. serverConfig.port = 48080;
  23. serverConfig.enableTls = true;
  24. // serverConfig.certificatePemFile = ...
  25. // serverConfig.keyPemFile = ...
  26. serverConfig.bindAddress = "127.0.0.1"; // to test IPv4 fallback
  27. serverConfig.maxMessageSize = 1000; // to test max message size
  28. WebSocketServer server(std::move(serverConfig));
  29. shared_ptr<WebSocket> client;
  30. server.onClient([&client](shared_ptr<WebSocket> incoming) {
  31. cout << "WebSocketServer: Client connection received" << endl;
  32. client = incoming;
  33. if (auto addr = client->remoteAddress())
  34. cout << "WebSocketServer: Client remote address is " << *addr << endl;
  35. client->onOpen([wclient = make_weak_ptr(client)]() {
  36. cout << "WebSocketServer: Client connection open" << endl;
  37. if (auto client = wclient.lock())
  38. if (auto path = client->path())
  39. cout << "WebSocketServer: Requested path is " << *path << endl;
  40. });
  41. client->onClosed([]() { cout << "WebSocketServer: Client connection closed" << endl; });
  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. const string myMessage = "Hello world from client";
  51. ws.onOpen([&ws, &myMessage]() {
  52. cout << "WebSocket: Open" << endl;
  53. ws.send(binary(1001, byte(0))); // test max message size
  54. ws.send(myMessage);
  55. });
  56. ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
  57. std::atomic<bool> received = false;
  58. std::atomic<bool> maxSizeReceived = false;
  59. ws.onMessage([&received, &maxSizeReceived, &myMessage](variant<binary, string> message) {
  60. if (holds_alternative<string>(message)) {
  61. string str = std::move(get<string>(message));
  62. if ((received = (str == myMessage)))
  63. cout << "WebSocket: Received expected message" << endl;
  64. else
  65. cout << "WebSocket: Received UNEXPECTED message" << endl;
  66. } else {
  67. binary bin = std::move(get<binary>(message));
  68. if ((maxSizeReceived = (bin.size() == 1000)))
  69. cout << "WebSocket: Received large message truncated at max size" << endl;
  70. else
  71. cout << "WebSocket: Received large message NOT TRUNCATED" << endl;
  72. }
  73. });
  74. ws.open("wss://localhost:48080/");
  75. int attempts = 15;
  76. while ((!ws.isOpen() || !received) && attempts--)
  77. this_thread::sleep_for(1s);
  78. if (!ws.isOpen())
  79. return TestResult(false, "WebSocket is not open");
  80. if (!received || !maxSizeReceived)
  81. return TestResult(false, "Expected messages not received");
  82. ws.close();
  83. this_thread::sleep_for(1s);
  84. server.stop();
  85. this_thread::sleep_for(1s);
  86. return TestResult(true);
  87. }
  88. #endif