websocket.c 3.5 KB

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