websocketserver.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. WebSocketServer::Configuration serverConfig;
  21. serverConfig.port = 48080;
  22. serverConfig.enableTls = true;
  23. // serverConfig.certificatePemFile = ...
  24. // serverConfig.keyPemFile = ...
  25. serverConfig.bindAddress = "127.0.0.1"; // to test IPv4 fallback
  26. serverConfig.maxMessageSize = 1000; // to test max message size
  27. WebSocketServer server(std::move(serverConfig));
  28. shared_ptr<WebSocket> client;
  29. server.onClient([&client](shared_ptr<WebSocket> incoming) {
  30. cout << "WebSocketServer: Client connection received" << endl;
  31. client = incoming;
  32. if(auto addr = client->remoteAddress())
  33. cout << "WebSocketServer: Client remote address is " << *addr << endl;
  34. client->onOpen([wclient = make_weak_ptr(client)]() {
  35. cout << "WebSocketServer: Client connection open" << endl;
  36. if(auto client = wclient.lock())
  37. if(auto path = client->path())
  38. cout << "WebSocketServer: Requested path is " << *path << endl;
  39. });
  40. client->onClosed([]() {
  41. cout << "WebSocketServer: Client connection closed" << endl;
  42. });
  43. client->onMessage([wclient = make_weak_ptr(client)](variant<binary, string> message) {
  44. if(auto client = wclient.lock())
  45. client->send(std::move(message));
  46. });
  47. });
  48. WebSocket::Configuration config;
  49. config.disableTlsVerification = true;
  50. WebSocket ws(std::move(config));
  51. const string myMessage = "Hello world from client";
  52. ws.onOpen([&ws, &myMessage]() {
  53. cout << "WebSocket: Open" << endl;
  54. ws.send(binary(1001, byte(0))); // test max message size
  55. ws.send(myMessage);
  56. });
  57. ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
  58. std::atomic<bool> received = false;
  59. std::atomic<bool> maxSizeReceived = false;
  60. ws.onMessage([&received, &maxSizeReceived, &myMessage](variant<binary, string> message) {
  61. if (holds_alternative<string>(message)) {
  62. string str = std::move(get<string>(message));
  63. if ((received = (str == myMessage)))
  64. cout << "WebSocket: Received expected message" << endl;
  65. else
  66. cout << "WebSocket: Received UNEXPECTED message" << endl;
  67. }
  68. else {
  69. binary bin = std::move(get<binary>(message));
  70. if ((maxSizeReceived = (bin.size() == 1000)))
  71. cout << "WebSocket: Received large message truncated at max size" << endl;
  72. else
  73. cout << "WebSocket: Received large message NOT TRUNCATED" << endl;
  74. }
  75. });
  76. ws.open("wss://localhost:48080/");
  77. int attempts = 15;
  78. while ((!ws.isOpen() || !received) && attempts--)
  79. this_thread::sleep_for(1s);
  80. if (!ws.isOpen())
  81. throw runtime_error("WebSocket is not open");
  82. if (!received || !maxSizeReceived)
  83. throw runtime_error("Expected messages not received");
  84. ws.close();
  85. this_thread::sleep_for(1s);
  86. server.stop();
  87. this_thread::sleep_for(1s);
  88. cout << "Success" << endl;
  89. }
  90. #endif