2
0

websocket.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 *exected_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(exected_payload)) {
  51. if(!memcmp(exected_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 int recv_any(CURL *curl)
  69. {
  70. size_t rlen;
  71. const struct curl_ws_frame *meta;
  72. char buffer[256];
  73. CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
  74. if(result)
  75. return result;
  76. return 0;
  77. }
  78. /* close the connection */
  79. static void websocket_close(CURL *curl)
  80. {
  81. size_t sent;
  82. (void)curl_ws_send(curl, "", 0, &sent, 0, CURLWS_CLOSE);
  83. }
  84. static void websocket(CURL *curl)
  85. {
  86. int i = 0;
  87. do {
  88. recv_any(curl);
  89. if(ping(curl, "foobar"))
  90. return;
  91. if(recv_pong(curl, "foobar")) {
  92. return;
  93. }
  94. sleep(2);
  95. } while(i++ < 10);
  96. websocket_close(curl);
  97. }
  98. int main(void)
  99. {
  100. CURL *curl;
  101. CURLcode res;
  102. curl = curl_easy_init();
  103. if(curl) {
  104. curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com");
  105. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
  106. /* Perform the request, res will get the return code */
  107. res = curl_easy_perform(curl);
  108. /* Check for errors */
  109. if(res != CURLE_OK)
  110. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  111. curl_easy_strerror(res));
  112. else {
  113. /* connected and ready */
  114. websocket(curl);
  115. }
  116. /* always cleanup */
  117. curl_easy_cleanup(curl);
  118. }
  119. return 0;
  120. }