websocket.c 3.5 KB

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