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

docs/examples/websocket: fix "socket not ready" on recv #14214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jay
Copy link
Member

@jay jay commented Jul 17, 2024

  • 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?

- 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 curl#13288
Closes #xxxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

websocket.c example: curl_ws_recv returns "Socket not ready for send/recv" (errno 81)
1 participant