test(WebSocket): simple ping/pong WebSocket client and server. (#2631)

This commit is contained in:
Matej Kenda 2024-03-26 17:10:58 +01:00
parent 482c066307
commit 6bff0307d3
3 changed files with 278 additions and 0 deletions

View File

@ -11,6 +11,10 @@ POCO_SOURCES_AUTO_PLAT(TEST_SRCS OFF
)
add_executable(NetSSL-testrunner ${TEST_SRCS})
add_executable(NetSSL-server ping/websocket-server.cpp)
add_executable(NetSSL-client ping/websocket-client.cpp)
if(ANDROID)
add_test(
NAME NetSSL
@ -32,5 +36,16 @@ else()
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/dhparams.pem ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/testrunner.xml ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/NetSSL-testrunner.xml
)
add_custom_command(
TARGET NetSSL-server POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/testrunner.xml ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/NetSSL-server.xml
)
add_custom_command(
TARGET NetSSL-client POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/testrunner.xml ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/NetSSL-client.xml
)
endif()
target_link_libraries(NetSSL-testrunner PUBLIC Poco::NetSSL Poco::Util Poco::XML CppUnit)
target_link_libraries(NetSSL-server PUBLIC Poco::NetSSL Poco::Util)
target_link_libraries(NetSSL-client PUBLIC Poco::NetSSL Poco::Util)

View File

@ -0,0 +1,101 @@
//
// websocket-client.cpp
//
// Copyright (c) 2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Util/Application.h"
using Poco::Net::HTTPSClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::WebSocket;
class PingClientApp: public Poco::Util::Application
{
public:
PingClientApp()
{
Poco::Net::initializeSSL();
}
~PingClientApp() override
{
Poco::Net::uninitializeSSL();
}
int main(const std::vector<std::string>& args) override
{
if (args.size() != 2)
{
std::cout << "Usage: " << this->commandName() << " hostname port" << std::endl;
return 1;
}
std::cout << "Connecting to: " << args[0] << ":" << args[1] << std::endl;
int port = std::stoi(args[1]);
HTTPSClientSession cs(args[0], port);
HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
HTTPResponse response;
WebSocket ws(cs, request, response);
char buffer[1024] = {};
int flags;
const std::string payload("PING!");
while (true)
{
std::cout << "Sending: " << payload << std::endl;
ws.sendFrame(payload.data(), (int) payload.size());
int n = ws.receiveFrame(buffer, sizeof(buffer), flags);
std::string response(buffer, n);
std::cout << "Received: " << response << std::endl;
Poco::Thread::current()->sleep(1000);
}
ws.shutdown();
ws.receiveFrame(buffer, sizeof(buffer), flags);
return 0;
}
void setup(int argc, char** argv)
{
init(argc, argv);
}
protected:
void initialize(Poco::Util::Application& self) override
{
loadConfiguration(); // load default configuration files, if present
Poco::Util::Application::initialize(self);
}
};
int main(int argc, char** argv)
{
PingClientApp app;
try
{
app.setup(argc, argv);
return app.run();
}
catch (Poco::Exception& exc)
{
std::cout << exc.displayText() << std::endl;
return 1;
}
}

View File

@ -0,0 +1,162 @@
//
// websocket-server.cpp
//
// Copyright (c) 2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/SecureServerSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Thread.h"
#include "Poco/Util/Application.h"
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPServerResponse;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
namespace
{
class WebSocketRequestHandler: public Poco::Net::HTTPRequestHandler
{
public:
WebSocketRequestHandler(std::size_t bufSize = 1024): _bufSize(bufSize)
{
}
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) override
{
try
{
WebSocket ws(request, response);
std::unique_ptr<char[]> pBuffer(new char[_bufSize]);
static const std::string pong("PONG!");
int flags;
int n;
do
{
n = ws.receiveFrame(pBuffer.get(), static_cast<int>(_bufSize), flags);
if (n == 0)
break;
std::string message(pBuffer.get(), n);
std::cout << "Received: " << message << " from " << request.clientAddress().toString() << std::endl;
ws.sendFrame(pong.c_str(), pong.size(), flags);
std::cout << "Replied: " << pong << std::endl;
}
while ((flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
}
catch (const WebSocketException& exc)
{
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
private:
std::size_t _bufSize;
};
class WebSocketRequestHandlerFactory: public Poco::Net::HTTPRequestHandlerFactory
{
public:
WebSocketRequestHandlerFactory(std::size_t bufSize = 1024): _bufSize(bufSize)
{
}
Poco::Net::HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request) override
{
return new WebSocketRequestHandler(_bufSize);
}
private:
std::size_t _bufSize;
};
}
class PingServerApp: public Poco::Util::Application
{
public:
PingServerApp()
{
Poco::Net::initializeSSL();
}
~PingServerApp() override
{
Poco::Net::uninitializeSSL();
}
int main(const std::vector<std::string>& args) override
{
std::cout << "Starting server" << std::endl;
Poco::Net::SecureServerSocket ss(0);
Poco::Net::HTTPServer server(new WebSocketRequestHandlerFactory, ss, new Poco::Net::HTTPServerParams);
server.start();
Poco::Thread::sleep(200);
std::cout << "Listening: " << ss.address().toString() << std::endl;
std::cout << "Serving requests. Press a key to stop." << std::endl;
std::cin.get();
server.stop();
return 0;
}
void setup(int argc, char** argv)
{
init(argc, argv);
}
protected:
void initialize(Poco::Util::Application& self) override
{
loadConfiguration(); // load default configuration files, if present
Poco::Util::Application::initialize(self);
}
};
int main(int argc, char** argv)
{
PingServerApp app;
try
{
app.setup(argc, argv);
return app.run();
}
catch (Poco::Exception& exc)
{
std::cout << exc.displayText() << std::endl;
return 1;
}
}