homework/cpp/try/uri.cpp

54 lines
1.4 KiB
C++
Raw Permalink Normal View History

2024-05-24 10:27:12 +00:00
#include <iostream>
#include <tuple>
#include <regex>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <jsoncpp/json/json.h>
namespace beast = boost::beast;
namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
std::tuple<std::string, std::string, std::string> parseURI(const std::string& uri) {
std::regex uriRegex(R"(^ws://([^:/]+):(\d+)(/.+)$)");
std::smatch match;
if (std::regex_match(uri, match, uriRegex)) {
return std::make_tuple(match[1].str(), match[2].str(), match[3].str());
} else {
throw std::invalid_argument("Nieprawidłowe URI");
}
}
int main() {
std::string uri = "ws://172.24.0.3:3333";
std::cout << "Dostarczone URI: " << uri << std::endl;
try {
auto uriParts = parseURI(uri);
std::string host, port, endpoint;
std::tie(host, port, endpoint) = uriParts;
net::io_context io_context;
// Utwórz obiekt WebSocket
websocket::stream<tcp::socket> ws(io_context);
// Połącz z serwerem WebSocket
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve(host, port);
net::connect(ws.next_layer(), endpoints);
// Wysyłanie danych na serwer WebSocket
ws.handshake(host, endpoint);
} catch (const std::exception& e) {
std::cerr << "Błąd: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}