Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
This repository was archived by the owner on Jun 1, 2026. It is now read-only.

How to set timeout for the socket connection #1777

Description

@DerXanRam

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();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions