• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ping(CURL * curl,const char * send_payload)33 static int ping(CURL *curl, const char *send_payload)
34 {
35   size_t sent;
36   CURLcode result =
37     curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
38                  CURLWS_PING);
39   return (int)result;
40 }
41 
recv_pong(CURL * curl,const char * expected_payload)42 static int recv_pong(CURL *curl, const char *expected_payload)
43 {
44   size_t rlen;
45   const struct curl_ws_frame *meta;
46   char buffer[256];
47   CURLcode result = curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
48   if(!result) {
49     if(meta->flags & CURLWS_PONG) {
50       int same = 0;
51       fprintf(stderr, "ws: got PONG back\n");
52       if(rlen == strlen(expected_payload)) {
53         if(!memcmp(expected_payload, buffer, rlen)) {
54           fprintf(stderr, "ws: got the same payload back\n");
55           same = 1;
56         }
57       }
58       if(!same)
59         fprintf(stderr, "ws: did NOT get the same payload back\n");
60     }
61     else {
62       fprintf(stderr, "recv_pong: got %u bytes rflags %x\n", (int)rlen,
63               meta->flags);
64     }
65   }
66   fprintf(stderr, "ws: curl_ws_recv returned %u, received %u\n",
67           (unsigned int)result, (unsigned int)rlen);
68   return (int)result;
69 }
70 
recv_any(CURL * curl)71 static CURLcode recv_any(CURL *curl)
72 {
73   size_t rlen;
74   const struct curl_ws_frame *meta;
75   char buffer[256];
76 
77   return curl_ws_recv(curl, buffer, sizeof(buffer), &rlen, &meta);
78 }
79 
80 /* close the connection */
websocket_close(CURL * curl)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 
websocket(CURL * curl)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 
main(void)102 int main(void)
103 {
104   CURL *curl;
105   CURLcode res;
106 
107   curl = curl_easy_init();
108   if(curl) {
109     curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com");
110 
111     curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L); /* websocket style */
112 
113     /* Perform the request, res gets the return code */
114     res = curl_easy_perform(curl);
115     /* Check for errors */
116     if(res != CURLE_OK)
117       fprintf(stderr, "curl_easy_perform() failed: %s\n",
118               curl_easy_strerror(res));
119     else {
120       /* connected and ready */
121       websocket(curl);
122     }
123 
124     /* always cleanup */
125     curl_easy_cleanup(curl);
126   }
127   return 0;
128 }
129