M
M
MasterCopipaster2021-05-25 17:26:57
C++ / C#
MasterCopipaster, 2021-05-25 17:26:57

How to properly organize the reception of data through WS?

Good day to all, I need to write an asynchronous WS client for reading data in real time, I chose boost for this task - and I successfully assembled this client example .
Actually the question is how to organize the reading of data, the example now connects to the WS server, sends something, receives a response and exits.
Explain the following things:
The code comments say the following:

// Run the I/O service. The call will return when
    // the socket is closed.
    ioc.run();

And here it is somehow strange why ioc.run(); will output data only when the connection is closed? - or I misunderstood something, because this is obvious nonsense.

How can I get data from WSta?
I want to get a string from the server and put it in a variable - every time the server sends me new data, in theory I need some kind of event loop
.
Maybe someone has an example of how the WS client works on boost?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2021-05-26
@MasterCopipaster

or i misunderstood something

Exactly.
You have chosen an asynchronous tool without understanding how it works.
ioc.run() - starts an asynchronous event loop. When it ends, you will no longer be able to receive / transmit anything.
Your task is to add the code you need to event handlers (on_xxx() functions). How to do it correctly, see the library documentation and possibly other examples of use.
I have never used boost.beast and WebSocket, but I have some experience with asynchronous programming, and the principles of asynchronous programming are the same in any libraries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question