I am trying to pass a program that uses websockets in C++ using the Boost library, to WebAssembly for its later use in a web application, or in its defect in a runtime like Wasmtime.
On the one hand I have used Emscripten to compile my C++ application, but I can't get it to work. I have seen that Emscripten has its own section associated to WebSockets, but my intention is to be able to take advantage of my C++ code that uses the Boost library to implement WebSockets.
On the other hand I have been reading about WASI, and maybe this could be a solution to my problem. I have been looking but I do not know if at the moment there is something implemented in reference to websocekts in C++, since what I have seen are proposals based on examples in Rust.
I do not know if someone could help me to solve this problem and to be able to transform my C++ code to WebAssembly and that this is functional in a web environment or in a runtime.
I leave a basic example of the C++ code that I am testing:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
int main(int argc, char** argv) {
try {
std::string host = "127.0.0.1";
std::string port = "3333";
std::string message = "Hello World!";
boost::asio::io_context ioContext;
boost::asio::ip::tcp::resolver resolver{ioContext};
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> webSocket{ioContext};
auto const results = resolver.resolve(host, port);
auto endPoint = boost::asio::connect(webSocket.next_layer(), results);
host += ':' + std::to_string(endPoint.port());
webSocket.set_option(boost::beast::websocket::stream_base::decorator(
[](boost::beast::websocket::request_type& req) {
req.set(boost::beast::http::field::user_agent,
std::string(BOOST_BEAST_VERSION_STRING) +
" websocket-client-coro");
}));
webSocket.handshake(host, "/");
webSocket.write(boost::asio::buffer(message));
boost::system::error_code errorCode;
webSocket.close(boost::beast::websocket::close_code::normal, errorCode);
}
catch(std::exception const& e) {
std::cerr << "There was an error: " << e.what() << std::endl;
return -1;
}
return 0;
}
Thanks