|
@@ -24,14 +24,13 @@ template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
|
|
void test_websocketserver() {
|
|
void test_websocketserver() {
|
|
InitLogger(LogLevel::Debug);
|
|
InitLogger(LogLevel::Debug);
|
|
|
|
|
|
- const string myMessage = "Hello world from client";
|
|
|
|
-
|
|
|
|
WebSocketServer::Configuration serverConfig;
|
|
WebSocketServer::Configuration serverConfig;
|
|
serverConfig.port = 48080;
|
|
serverConfig.port = 48080;
|
|
serverConfig.enableTls = true;
|
|
serverConfig.enableTls = true;
|
|
// serverConfig.certificatePemFile = ...
|
|
// serverConfig.certificatePemFile = ...
|
|
// serverConfig.keyPemFile = ...
|
|
// serverConfig.keyPemFile = ...
|
|
serverConfig.bindAddress = "127.0.0.1"; // to test IPv4 fallback
|
|
serverConfig.bindAddress = "127.0.0.1"; // to test IPv4 fallback
|
|
|
|
+ serverConfig.maxMessageSize = 1000; // to test max message size
|
|
WebSocketServer server(std::move(serverConfig));
|
|
WebSocketServer server(std::move(serverConfig));
|
|
|
|
|
|
shared_ptr<WebSocket> client;
|
|
shared_ptr<WebSocket> client;
|
|
@@ -63,15 +62,19 @@ void test_websocketserver() {
|
|
config.disableTlsVerification = true;
|
|
config.disableTlsVerification = true;
|
|
WebSocket ws(std::move(config));
|
|
WebSocket ws(std::move(config));
|
|
|
|
|
|
|
|
+ const string myMessage = "Hello world from client";
|
|
|
|
+
|
|
ws.onOpen([&ws, &myMessage]() {
|
|
ws.onOpen([&ws, &myMessage]() {
|
|
cout << "WebSocket: Open" << endl;
|
|
cout << "WebSocket: Open" << endl;
|
|
|
|
+ ws.send(binary(1001, byte(0))); // test max message size
|
|
ws.send(myMessage);
|
|
ws.send(myMessage);
|
|
});
|
|
});
|
|
|
|
|
|
ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
|
|
ws.onClosed([]() { cout << "WebSocket: Closed" << endl; });
|
|
|
|
|
|
std::atomic<bool> received = false;
|
|
std::atomic<bool> received = false;
|
|
- ws.onMessage([&received, &myMessage](variant<binary, string> message) {
|
|
|
|
|
|
+ std::atomic<bool> maxSizeReceived = false;
|
|
|
|
+ ws.onMessage([&received, &maxSizeReceived, &myMessage](variant<binary, string> message) {
|
|
if (holds_alternative<string>(message)) {
|
|
if (holds_alternative<string>(message)) {
|
|
string str = std::move(get<string>(message));
|
|
string str = std::move(get<string>(message));
|
|
if ((received = (str == myMessage)))
|
|
if ((received = (str == myMessage)))
|
|
@@ -79,6 +82,13 @@ void test_websocketserver() {
|
|
else
|
|
else
|
|
cout << "WebSocket: Received UNEXPECTED message" << endl;
|
|
cout << "WebSocket: Received UNEXPECTED message" << endl;
|
|
}
|
|
}
|
|
|
|
+ else {
|
|
|
|
+ binary bin = std::move(get<binary>(message));
|
|
|
|
+ if ((maxSizeReceived = (bin.size() == 1000)))
|
|
|
|
+ cout << "WebSocket: Received large message truncated at max size" << endl;
|
|
|
|
+ else
|
|
|
|
+ cout << "WebSocket: Received large message NOT TRUNCATED" << endl;
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
ws.open("wss://localhost:48080/");
|
|
ws.open("wss://localhost:48080/");
|
|
@@ -90,8 +100,8 @@ void test_websocketserver() {
|
|
if (!ws.isOpen())
|
|
if (!ws.isOpen())
|
|
throw runtime_error("WebSocket is not open");
|
|
throw runtime_error("WebSocket is not open");
|
|
|
|
|
|
- if (!received)
|
|
|
|
- throw runtime_error("Expected message not received");
|
|
|
|
|
|
+ if (!received || !maxSizeReceived)
|
|
|
|
+ throw runtime_error("Expected messages not received");
|
|
|
|
|
|
ws.close();
|
|
ws.close();
|
|
this_thread::sleep_for(1s);
|
|
this_thread::sleep_for(1s);
|