1

Websocket connection stage, curl_multi_poll() is useful for monitoring. However, during the data transmission and reception stage, curl_multi_poll() fails.

It is found that the CURL_POLL_REMOVE event occurs after the websocket connection is successful.

How can this problem be solved?

curl_easy_setopt(easy, CURLOPT_URL, "ws://xxx.com.cn");
curl_easy_setopt(easy, CURLOPT_CONNECT_ONLY, 2L); // 2 => WebSocket(1 => TCP)
curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS); // 可选,TLS/HTTP2
curl_easy_setopt(easy, CURLOPT_TIMEOUT, 30L);
curl_multi_add_handle(multi, easy);
int running = 0;
curl_multi_perform(multi, &running);
while (running) {
 int numfds = 0;
 CURLMcode mc = curl_multi_poll(multi, NULL, 0, 1000, &numfds);
 if (mc != CURLM_OK) break;
 curl_multi_perform(multi, &running);
}
curl_ws_send(easy, msg, strlen(msg), &sent, 0, CURLWS_TEXT);
rc = curl_ws_recv(easy, buf, sizeof(buf), &rlen,&meta);
if (rc == CURLE_AGAIN) {
 /* error code, Even if data is received, it will cause a 1000ms delay. */
 curl_multi_poll(multi, NULL, 0, 1000, &numfds); 
}
3CEZVQ
41.8k11 gold badges90 silver badges98 bronze badges
asked Sep 30 at 8:16
3
  • Use CURLINFO_ACTIVESOCKET + your own event loop instead of relying on curl_multi_poll() post-upgrade. Commented Sep 30 at 8:26
  • Does curl_multi_poll() not support the transmission and reception of WebSocket data? How to use CURLINFO_ACTIVESOCKET? Here's a simple example. I need to do it in a non-polling manner. Commented Sep 30 at 8:32
  • The first comment is very poor. Commented Sep 30 at 9:03

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.