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