websocket.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 using CONNECT_ONLY
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <curl/curl.h>
  32. static int ping(CURL *curl, const char *send_payload)
  33. {
  34. size_t sent;
  35. CURLcode result =
  36. curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
  37. CURLWS_PING);
  38. return (int)result;
  39. }
  40. static int recv_pong(CURL *curl, const char *expected_payload)
  41. {
  42. size_t rlen;
  43. const struct curl_ws_frame *meta;
  44. char buffer[256];
  45. CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
  46. if(!result) {
  47. if(meta->flags & CURLWS_PONG) {
  48. int same = 0;
  49. fprintf(stderr, "ws: got PONG back\n");
  50. if(rlen == strlen(expected_payload)) {
  51. if(!memcmp(expected_payload, buffer, rlen)) {
  52. fprintf(stderr, "ws: got the same payload back\n");
  53. same = 1;
  54. }
  55. }
  56. if(!same)
  57. fprintf(stderr, "ws: did NOT get the same payload back\n");
  58. }
  59. else {
  60. fprintf(stderr, "recv_pong: got %u bytes rflags %x\n", (int)rlen,
  61. meta->flags);
  62. }
  63. }
  64. fprintf(stderr, "ws: curl_ws_recv returned %u, received %u\n",
  65. (unsigned int)result, (unsigned int)rlen);
  66. return (int)result;
  67. }
  68. static CURLcode recv_any(CURL *curl)
  69. {
  70. size_t rlen;
  71. const struct curl_ws_frame *meta;
  72. char buffer[256];
  73. return curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
  74. }
  75. /* close the connection */
  76. static void websocket_close(CURL *curl)
  77. {
  78. size_t sent;
  79. (void)curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
  80. }
  81. static void websocket(CURL *curl)
  82. {
  83. int i = 0;
  84. do {
  85. recv_any(curl);
  86. if(ping(curl, "foobar"))
  87. return;
  88. if(recv_pong(curl, "foobar")) {
  89. return;
  90. }
  91. sleep(2);
  92. } while(i++ < 10);
  93. websocket_close(curl);
  94. }
  95. int main(void)
  96. {
  97. CURL *curl;
  98. CURLcode res;
  99. curl = curl_easy_init();
  100. if(curl) {
  101. curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com");
  102. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  103. /* Perform the request, res gets the return code */
  104. res = curl_easy_perform(curl);
  105. /* Check for errors */
  106. if(res != CURLE_OK)
  107. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  108. curl_easy_strerror(res));
  109. else {
  110. /* connected and ready */
  111. websocket(curl);
  112. }
  113. /* always cleanup */
  114. curl_easy_cleanup(curl);
  115. }
  116. return 0;
  117. }