xref: /aosp_15_r20/external/curl/docs/libcurl/curl_ws_recv.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: curl_ws_recv
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerSee-also:
8*6236dae4SAndroid Build Coastguard Worker  - curl_easy_getinfo (3)
9*6236dae4SAndroid Build Coastguard Worker  - curl_easy_perform (3)
10*6236dae4SAndroid Build Coastguard Worker  - curl_easy_setopt (3)
11*6236dae4SAndroid Build Coastguard Worker  - curl_ws_send (3)
12*6236dae4SAndroid Build Coastguard Worker  - libcurl-ws (3)
13*6236dae4SAndroid Build Coastguard WorkerProtocol:
14*6236dae4SAndroid Build Coastguard Worker  - WS
15*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.86.0
16*6236dae4SAndroid Build Coastguard Worker---
17*6236dae4SAndroid Build Coastguard Worker
18*6236dae4SAndroid Build Coastguard Worker# NAME
19*6236dae4SAndroid Build Coastguard Worker
20*6236dae4SAndroid Build Coastguard Workercurl_ws_recv - receive WebSocket data
21*6236dae4SAndroid Build Coastguard Worker
22*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
23*6236dae4SAndroid Build Coastguard Worker
24*6236dae4SAndroid Build Coastguard Worker~~~c
25*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
26*6236dae4SAndroid Build Coastguard Worker
27*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_ws_recv(CURL *curl, void *buffer, size_t buflen,
28*6236dae4SAndroid Build Coastguard Worker                      size_t *recv, const struct curl_ws_frame **meta);
29*6236dae4SAndroid Build Coastguard Worker~~~
30*6236dae4SAndroid Build Coastguard Worker
31*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
32*6236dae4SAndroid Build Coastguard Worker
33*6236dae4SAndroid Build Coastguard WorkerThis function call is EXPERIMENTAL.
34*6236dae4SAndroid Build Coastguard Worker
35*6236dae4SAndroid Build Coastguard WorkerRetrieves as much as possible of a received WebSocket data fragment into the
36*6236dae4SAndroid Build Coastguard Worker**buffer**, but not more than **buflen** bytes. *recv* is set to the
37*6236dae4SAndroid Build Coastguard Workernumber of bytes actually stored.
38*6236dae4SAndroid Build Coastguard Worker
39*6236dae4SAndroid Build Coastguard WorkerIf there is more fragment data to deliver than what fits in the provided
40*6236dae4SAndroid Build Coastguard Worker*buffer*, libcurl returns a full buffer and the application needs to call this
41*6236dae4SAndroid Build Coastguard Workerfunction again to continue draining the buffer.
42*6236dae4SAndroid Build Coastguard Worker
43*6236dae4SAndroid Build Coastguard WorkerIf the function call is successful, the *meta* pointer gets set to point to a
44*6236dae4SAndroid Build Coastguard Worker*const struct curl_ws_frame* that contains information about the received
45*6236dae4SAndroid Build Coastguard Workerdata. That struct must not be freed and its contents must not be relied upon
46*6236dae4SAndroid Build Coastguard Workeranymore once another WebSocket function is called. See the curl_ws_meta(3) for
47*6236dae4SAndroid Build Coastguard Workerdetails on that struct.a
48*6236dae4SAndroid Build Coastguard Worker
49*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
50*6236dae4SAndroid Build Coastguard Worker
51*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
52*6236dae4SAndroid Build Coastguard Worker
53*6236dae4SAndroid Build Coastguard Worker~~~c
54*6236dae4SAndroid Build Coastguard Workerint main(void)
55*6236dae4SAndroid Build Coastguard Worker{
56*6236dae4SAndroid Build Coastguard Worker  size_t rlen;
57*6236dae4SAndroid Build Coastguard Worker  const struct curl_ws_frame *meta;
58*6236dae4SAndroid Build Coastguard Worker  char buffer[256];
59*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
60*6236dae4SAndroid Build Coastguard Worker  if(curl) {
61*6236dae4SAndroid Build Coastguard Worker    CURLcode res = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
62*6236dae4SAndroid Build Coastguard Worker    if(res)
63*6236dae4SAndroid Build Coastguard Worker      printf("error: %s\n", curl_easy_strerror(res));
64*6236dae4SAndroid Build Coastguard Worker  }
65*6236dae4SAndroid Build Coastguard Worker}
66*6236dae4SAndroid Build Coastguard Worker~~~
67*6236dae4SAndroid Build Coastguard Worker
68*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
69*6236dae4SAndroid Build Coastguard Worker
70*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
71*6236dae4SAndroid Build Coastguard Worker
72*6236dae4SAndroid Build Coastguard WorkerReturns **CURLE_OK** if everything is okay, and a non-zero number for
73*6236dae4SAndroid Build Coastguard Workererrors. Returns **CURLE_GOT_NOTHING** if the associated connection is
74*6236dae4SAndroid Build Coastguard Workerclosed.
75*6236dae4SAndroid Build Coastguard Worker
76*6236dae4SAndroid Build Coastguard WorkerInstead of blocking, the function returns **CURLE_AGAIN**. The correct
77*6236dae4SAndroid Build Coastguard Workerbehavior is then to wait for the socket to signal readability before calling
78*6236dae4SAndroid Build Coastguard Workerthis function again.
79