xref: /curl/docs/libcurl/curl_ws_send.md (revision 3e64569a)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_ws_send
5Section: 3
6Source: libcurl
7See-also:
8  - curl_easy_getinfo (3)
9  - curl_easy_perform (3)
10  - curl_easy_setopt (3)
11  - curl_ws_recv (3)
12  - libcurl-ws (3)
13Protocol:
14  - WS
15Added-in: 7.86.0
16---
17
18# NAME
19
20curl_ws_send - send WebSocket data
21
22# SYNOPSIS
23
24~~~c
25#include <curl/curl.h>
26
27CURLcode curl_ws_send(CURL *curl, const void *buffer, size_t buflen,
28                      size_t *sent, curl_off_t fragsize,
29                      unsigned int flags);
30~~~
31
32# DESCRIPTION
33
34This function call is EXPERIMENTAL.
35
36Send the specific message fragment over an established WebSocket
37connection. The *buffer* holds the data to send and it is *buflen*
38number of payload bytes in that memory area.
39
40*sent* is returned as the number of payload bytes actually sent.
41
42To send a (huge) fragment using multiple calls with partial content per
43invoke, set the *CURLWS_OFFSET* bit and the *fragsize* argument as the
44total expected size for the first part, then set the *CURLWS_OFFSET* with
45a zero *fragsize* for the following parts.
46
47If not sending a partial fragment or if this is raw mode, *fragsize*
48should be set to zero.
49
50If **CURLWS_RAW_MODE** is enabled in CURLOPT_WS_OPTIONS(3), the
51**flags** argument should be set to 0.
52
53To send a message consisting of multiple frames, set the *CURLWS_CONT* bit
54in all frames except the final one.
55
56Warning: while it is possible to invoke this function from a callback,
57such a call is blocking in this situation, e.g. only returns after all data
58has been sent or an error is encountered.
59
60# FLAGS
61
62## CURLWS_TEXT
63
64The buffer contains text data. Note that this makes a difference to WebSocket
65but libcurl itself does not make any verification of the content or
66precautions that you actually send valid UTF-8 content.
67
68## CURLWS_BINARY
69
70This is binary data.
71
72## CURLWS_CONT
73
74This is not the final fragment of the message, which implies that there is
75another fragment coming as part of the same message where this bit is not set.
76
77## CURLWS_CLOSE
78
79Close this transfer.
80
81## CURLWS_PING
82
83This is a ping.
84
85## CURLWS_PONG
86
87This is a pong.
88
89## CURLWS_OFFSET
90
91The provided data is only a partial fragment and there is more coming in a
92following call to *curl_ws_send()*. When sending only a piece of the
93fragment like this, the *fragsize* must be provided with the total
94expected fragment size in the first call and it needs to be zero in subsequent
95calls.
96
97# %PROTOCOLS%
98
99# EXAMPLE
100
101~~~c
102#include <string.h> /* for strlen */
103
104const char *send_payload = "magic";
105
106int main(void)
107{
108  size_t sent;
109  CURLcode res;
110  CURL *curl = curl_easy_init();
111  curl_easy_setopt(curl, CURLOPT_URL, "wss://example.com/");
112  curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 2L);
113  curl_easy_perform(curl);
114  res = curl_ws_send(curl, send_payload, strlen(send_payload), &sent, 0,
115                     CURLWS_PING);
116  curl_easy_cleanup(curl);
117  return (int)res;
118}
119~~~
120
121# %AVAILABILITY%
122
123# RETURN VALUE
124
125*CURLE_OK* (zero) means that the data was sent properly, non-zero means an
126error occurred as *\<curl/curl.h\>* defines. See the libcurl-errors(3) man
127page for the full list with descriptions.
128