Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set timeout for the socket connection #1777

Open
DerXanRam opened this issue Nov 3, 2023 · 1 comment
Open

How to set timeout for the socket connection #1777

DerXanRam opened this issue Nov 3, 2023 · 1 comment

Comments

@DerXanRam
Copy link

DerXanRam commented Nov 3, 2023

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
Copy link
Author

for the love of God please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant