2
0

websocket-cb.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. static size_t writecb(char *b, size_t size, size_t nitems, void *p)
  31. {
  32. CURL *easy = p;
  33. size_t i;
  34. const struct curl_ws_frame *frame = curl_ws_meta(easy);
  35. fprintf(stderr, "Type: %s\n", frame->flags & CURLWS_BINARY ?
  36. "binary" : "text");
  37. fprintf(stderr, "Bytes: %u", (unsigned int)(nitems * size));
  38. for(i = 0; i < nitems; i++)
  39. fprintf(stderr, "%02x ", (unsigned char)b[i]);
  40. return nitems;
  41. }
  42. int main(void)
  43. {
  44. CURL *curl;
  45. CURLcode res;
  46. curl = curl_easy_init();
  47. if(curl) {
  48. curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com");
  49. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writecb);
  50. /* pass the easy handle to the callback */
  51. curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl);
  52. /* Perform the request, res gets the return code */
  53. res = curl_easy_perform(curl);
  54. /* Check for errors */
  55. if(res != CURLE_OK)
  56. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  57. curl_easy_strerror(res));
  58. /* always cleanup */
  59. curl_easy_cleanup(curl);
  60. }
  61. return 0;
  62. }