1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 /* <DESC>
25 * WebSocket download-only using write callback
26 * </DESC>
27 */
28 #include <stdio.h>
29 #include <curl/curl.h>
30
writecb(char * b,size_t size,size_t nitems,void * p)31 static size_t writecb(char *b, size_t size, size_t nitems, void *p)
32 {
33 CURL *easy = p;
34 size_t i;
35 const struct curl_ws_frame *frame = curl_ws_meta(easy);
36 fprintf(stderr, "Type: %s\n", frame->flags & CURLWS_BINARY ?
37 "binary" : "text");
38 fprintf(stderr, "Bytes: %u", (unsigned int)(nitems * size));
39 for(i = 0; i < nitems; i++)
40 fprintf(stderr, "%02x ", (unsigned char)b[i]);
41 return nitems;
42 }
43
main(void)44 int main(void)
45 {
46 CURL *curl;
47 CURLcode res;
48
49 curl = curl_easy_init();
50 if(curl) {
51 curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com");
52
53 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
54 /* pass the easy handle to the callback */
55 curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
56
57 /* Perform the request, res gets the return code */
58 res = curl_easy_perform(curl);
59 /* Check for errors */
60 if(res != CURLE_OK)
61 fprintf(stderr, "curl_easy_perform() failed: %s\n",
62 curl_easy_strerror(res));
63
64 /* always cleanup */
65 curl_easy_cleanup(curl);
66 }
67 return 0;
68 }
69