I tried following the above example and ran into a compilation error; namely, code C2039: 'type': is not a member of 'boost::asio::result_of<Function (boost::asio::basic_yield_context<Executor>)>'
.
The modifications to the example provided above are attached here:
Header:
namespace NetTools {
class ClientSession
: public std::enable_shared_from_this<ClientSession>
{
private:
std::string auth_token;
std::shared_ptr<spdlog::logger> m_pLog;
public:
ClientSession(
std::string auth_token, std::shared_ptr<spdlog::logger> log)
: auth_token(std::move(auth_token))
, m_pLog(std::move(log))
{
}
int StartSession();
void do_session(
std::string host,
std::string const& port,
std::string const& text,
net::io_context& ioc,
ssl::context& ctx,
net::yield_context yield);
};
do_session
modification:
// Sends a WebSocket message and prints the response
void NetTools::ClientSession::do_session(
std::string host,
std::string const& port,
std::string const& text,
net::io_context& ioc,
ssl::context& ctx,
net::yield_context yield)
{
beast::error_code ec;
// These objects perform our I/O
// ... etc
main
function replacement:
int NetTools::ClientSession::StartSession()
{
auto const host = "echo.websocket.events";
auto const port = "https";
auto const text = "Hello world!";
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::tlsv12_client};
// This holds the root certificate used for verification
load_root_certificates(ctx);
// Launch the asynchronous operation
net::spawn(
ioc,
std::bind(
&NetTools::ClientSession::do_session,
std::string(host),
std::string(port),
std::string(text),
std::ref(ioc),
std::ref(ctx),
std::placeholders::_1));
The function bodies are unmodified; I replaced the main function with a StartSession function which I call in a test file (not shown). I am not sure why this error occurs and prevents my code from compiling.
CPP Version: 17
Boost (and all related beast packages) Version: 1.8.0
OS: Windows 11 x64
Tried: Running code above as described.
Expectation: Compiles and runs (though at minimum compiles!)
Result: Does not compile with error code C2039: 'type': is not a member of 'boost::asio::result_of<Function (boost::asio::basic_yield_context)>'.
You made
do_session
a non-static member function of a class. This means that you need an instance of theClientSession
object to invoke it on. You don't pass one in thestd::bind()
expression, which leads to confused error messages because there is no validasync_result<>
specialization for your handler type. In fact, your handler type is simply invalid.Make an instance, and use it:
Then inside
StartSession
you can use the current instance,this
:All code rolled into one:
Live On Coliru
Running locally against the https://echo.websocket.events/.ws service: