docs/examples/websocket: fix "socket not ready" on recv #14214
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overhaul the recv_any function to block waiting for a complete websocket message from the server.
Have recv_pong and recv_header call recv_any to retrieve a complete message, rather than duplicate the efforts in each function.
Fix sleep() macro for Windows to treat the parameter as seconds instead of milliseconds.
Improve documentation of curl_ws_recv/send to better explain CURLE_AGAIN behavior.
Use echo.websocket.org instead of example.com as the example websocket server since it is a server the example actually works on.
recv_any now calls curl_ws_recv repeatedly, waiting until the socket is readable and a full websocket message is read from the server.
Prior to this change the example did not handle the non-blocking behavior of curl_ws_recv. When curl_ws_recv returned CURLE_AGAIN the example would fail with "socket not ready".
Fixes #13288
Closes #xxxx
This is the second take. The first take is in jay:curl:fix_websocket_example__alternate_take. The first take is more thorough because I wrote a blocking function that can return incomplete non-control messages when they are disrupted by a control message. However after I wrote it I thought it was too complicated for the example.
This only fixes the example for recv. Neither take covers partial sends. It's unclear to me what to do in that case. For example let's say curl_ws_send is called to send a PING with a payload of 6 bytes, but when it returns only 5 of the 6 bytes of payload were sent. What is the caller supposed to do in that case, has the PING been sent with just the 5 bytes or is it waiting to send the remaining byte and curl_ws_send has to be called again with offset?