How to connect to websocket server using boost C++

Viewed 1112

My websocket server URL is ws://localhost/webstream/wsocket Iam trying to create a C++ websocket client that connects to this server using boost

tcp::resolver resolver{ioc};
_pws = new websocket::stream<tcp::socket>(ioc);

// Look up the domain name
// my server is http://localhost/webstream/wsocket
_host = "localhost";
_port = 80;

auto const results = resolver.resolve(_host, std::to_string(_port));
if(results.size() == 0)
{
    std::cout<<"failed to connect to websocket server"<<std::endl;
    delete _pws;
    _pws = nullptr;
    return;
}

// Make the connection on the IP address we get from a lookup
net::connect(_pws->next_layer(), results.begin(), results.end());

// Set a decorator to change the User-Agent of the handshake
_pws->set_option(websocket::stream_base::decorator(
    [](websocket::request_type& req)
    {
        req.set(http::field::user_agent,
            std::string(BOOST_BEAST_VERSION_STRING) +
                " websocket-client-coro");
    }));
_pws->handshake(host, "/");

But iam unable to connect to sever. How can i set the path "/webstream/wsocket" to connect. '''

What I have tried:

i tried with
_host = "localhost/webstream/wsocket";
_port = 80;

_pws->handshake(host, "/webstream/wsocket");

but not getting connected In boost library, how can we specify the path of websocket server

2 Answers

This is an example I used to connect to my url. I was sending string data from my client, and reading it in from the port using my server.

If you were able to connect to it using postman, I'm sure this will work.


    auto const address = boost::asio::ip::make_address("127.0.0.1");
    auto const port = static_cast<unsigned short>(std::atoi("9898"));

    boost::asio::io_context ioc { 1 };

    tcp::acceptor acceptor { ioc, { address, port } };
    char againStart;

    while (1) {
        tcp::socket socket { ioc };
        acceptor.accept(socket);
        std::cout << "socket accepted" << std::endl;

        std::thread { [q = std::move(socket)]() mutable {

            boost::beast::websocket::stream<tcp::socket> ws { std::move(q) };

            ws.accept();

            while (1) {
                boost::beast::flat_buffer buffer;

                ws.read(buffer);
            

                auto out = boost::beast::buffers_to_string(buffer.cdata());

                stringVal = out;

                int argument = argument + 1;
                ofstream file;
                file.open("filePath.txt", ios::out | ios::app); //, ios::out | ios::app used to add to another line instead of changing entire file
                file << stringVal;
                file << argument << endl;
                file.close();

            }

        } }.detach();

I also kept some of the lines I used to read in data from the buffer. In theory this should work with your url as its client server binding.

Similar to this issue

The right way to access the websocket url: ws://localhost:5000/webstream/wsocket , you need to set

 host_ = "localhost";
 port_ = 5000;

after resolve the endpoint and connect, the path /webstream/wsocket was needed at handshake

_pws->handshake(host, "/webstream/wsocket")

In this way you may successfully establish the websocket connection

Related