cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

How to set timeout for the socket connection

Open DerXanRam opened this issue 2 years ago • 1 comments

Hello guys. I'm using this lib for subscribing web socket and i want to set timeout for the socket connection. Cause sometimes the receive call back function stops receiving data and freezes. This is my code

void sock()
{
    websocket_callback_client client;

    client.connect("wss://bsc-mainnet.infura.io/xxxx-xxx--xxxxx").wait();
    std::string body_str = R"({"jsonrpc":"2.0","method":"eth_subscribe","params":["newPendingTransactions"],"id":1})";
    std::vector<unsigned char> body(body_str.begin(), body_str.end());

    pplx::task_completion_event<void> receiveEvent;
    // make sure client works fine without setting receive handler
    websocket_outgoing_message msg;
    msg.set_utf8_message(body_str);
    client.send(msg).wait();

    // set receive handler
    client.set_message_handler([body_str, &receiveEvent](websocket_incoming_message ret_msg)
                               {
            auto body = ret_msg.extract_string().get();
             ret_str = body;
             cout << j << " " << ret_str;
             j++;
            if(false)
            {
                receiveEvent.set();
            } });

    client.send(msg);

    pplx::create_task(receiveEvent).wait();
    client.close().wait();
}

DerXanRam avatar Nov 03 '23 19:11 DerXanRam

for the love of God please help

DerXanRam avatar Nov 05 '23 10:11 DerXanRam