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
lang-c
CURLINFO_ACTIVESOCKET
+ your own event loop instead of relying oncurl_multi_poll()
post-upgrade.