2024-01-17 18:32:44 +08:00
|
|
|
---
|
2024-02-28 18:28:10 +08:00
|
|
|
c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2024-01-17 18:32:44 +08:00
|
|
|
SPDX-License-Identifier: curl
|
|
|
|
Title: libcurl
|
|
|
|
Section: 3
|
|
|
|
Source: libcurl
|
|
|
|
See-also:
|
|
|
|
- CURLOPT_CONNECT_ONLY (3)
|
|
|
|
- CURLOPT_WRITEFUNCTION (3)
|
|
|
|
- CURLOPT_WS_OPTIONS (3)
|
|
|
|
- curl_easy_init (3)
|
|
|
|
- curl_ws_meta (3)
|
|
|
|
- curl_ws_recv (3)
|
|
|
|
- curl_ws_send (3)
|
2024-03-21 18:50:20 +08:00
|
|
|
Protocol:
|
2024-03-23 06:48:54 +08:00
|
|
|
- All
|
2024-01-17 18:32:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
# NAME
|
|
|
|
|
|
|
|
libcurl-ws - WebSocket interface overview
|
|
|
|
|
|
|
|
# DESCRIPTION
|
|
|
|
|
2023-06-14 16:57:50 +08:00
|
|
|
The WebSocket interface provides functions for receiving and sending WebSocket
|
|
|
|
data.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# INCLUDE
|
|
|
|
|
2024-02-06 06:23:56 +08:00
|
|
|
You still only include \<curl/curl.h\> in your code.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# SETUP
|
|
|
|
|
|
|
|
WebSocket is also often known as *WebSockets*, in plural. It is done by
|
2023-06-14 16:57:50 +08:00
|
|
|
upgrading a regular HTTP(S) GET request to a WebSocket connection.
|
|
|
|
|
|
|
|
WebSocket is a TCP-like message-based communication protocol done over HTTP,
|
|
|
|
specified in RFC 6455.
|
|
|
|
|
|
|
|
To initiate a WebSocket session with libcurl, setup an easy handle to use a
|
|
|
|
URL with a "WS://" or "WSS://" scheme. "WS" is for cleartext communication
|
|
|
|
over HTTP and "WSS" is for doing WebSocket securely over HTTPS.
|
|
|
|
|
|
|
|
A WebSocket request is done as an HTTP/1 GET request with an "Upgrade
|
|
|
|
WebSocket" request header field. When the upgrade is accepted by the server,
|
|
|
|
it responds with a 101 Switching and then the client can speak WebSocket with
|
2023-06-15 05:38:22 +08:00
|
|
|
the server. The communication can happen in both directions at the same time.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# MESSAGES
|
|
|
|
|
2023-06-14 19:30:50 +08:00
|
|
|
WebSocket communication is message based. That means that both ends send and
|
|
|
|
receive entire messages, not streams like TCP. A WebSocket message is sent
|
2023-06-14 16:57:50 +08:00
|
|
|
over the wire in one or more frames. Each frame in a message can have a size
|
|
|
|
up to 2^63 bytes.
|
|
|
|
|
|
|
|
libcurl delivers WebSocket data as frame fragments. It might send a whole
|
|
|
|
frame, but it might also deliver them in pieces depending on size and network
|
|
|
|
patterns. It makes sure to provide the API user about the exact specifics
|
|
|
|
about the fragment: type, offset, size and how much data there is pending to
|
|
|
|
arrive for the same frame.
|
|
|
|
|
|
|
|
A message has an unknown size until the last frame header for the message has
|
|
|
|
been received since only frames have set sizes.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# Raw mode
|
|
|
|
|
2023-06-19 02:57:29 +08:00
|
|
|
libcurl can be told to speak WebSocket in "raw mode" by setting the
|
2024-01-17 18:32:44 +08:00
|
|
|
**CURLWS_RAW_MODE** bit to the CURLOPT_WS_OPTIONS(3) option.
|
2023-06-19 02:57:29 +08:00
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
Raw WebSocket means that libcurl passes on the data from the network without
|
|
|
|
parsing it leaving that entirely to the application. This mode assumes that
|
|
|
|
the user of this knows WebSocket and can parse and figure out the data all by
|
|
|
|
itself.
|
2023-06-19 02:57:29 +08:00
|
|
|
|
|
|
|
This mode is intended for applications that already have a WebSocket
|
|
|
|
parser/engine that want to switch over to use libcurl for enabling WebSocket,
|
2024-01-17 18:32:44 +08:00
|
|
|
and keep parts of the existing software architecture.
|
|
|
|
|
|
|
|
# PING
|
|
|
|
|
2023-06-14 16:57:50 +08:00
|
|
|
WebSocket is designed to allow long-lived sessions and in order to keep the
|
|
|
|
connections alive, both ends can send PING messages for the other end to
|
|
|
|
respond with a PONG.
|
|
|
|
|
|
|
|
libcurl automatically responds to server PING messages with a PONG. It does
|
|
|
|
not send any PING messages automatically.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# MODELS
|
|
|
|
|
2023-06-14 16:57:50 +08:00
|
|
|
Because of the many different ways WebSocket can be used, which is much more
|
|
|
|
flexible than limited to plain downloads or uploads, libcurl offers two
|
|
|
|
different API models to use it:
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
1. Using a write callback with CURLOPT_WRITEFUNCTION(3) much like other
|
2023-06-14 16:57:50 +08:00
|
|
|
downloads for when the traffic is download oriented.
|
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
2. Using CURLOPT_CONNECT_ONLY(3) and use the WebSocket recv/send
|
2023-08-22 23:40:39 +08:00
|
|
|
functions.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# Callback model
|
|
|
|
|
2023-06-14 16:57:50 +08:00
|
|
|
When a write callback is set and a WebSocket transfer is performed, the
|
2023-08-22 23:40:39 +08:00
|
|
|
callback is called to deliver all WebSocket data that arrives.
|
2023-06-14 16:57:50 +08:00
|
|
|
|
2024-01-17 18:32:44 +08:00
|
|
|
The callback can then call curl_ws_meta(3) to learn about the details of
|
2023-06-14 16:57:50 +08:00
|
|
|
the incoming data fragment.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# CONNECT_ONLY model
|
|
|
|
|
|
|
|
By setting CURLOPT_CONNECT_ONLY(3) to **2L**, the transfer only
|
2023-08-22 23:40:39 +08:00
|
|
|
establishes and setups the WebSocket communication and then returns control
|
|
|
|
back to the application.
|
2023-06-14 16:57:50 +08:00
|
|
|
|
|
|
|
Once such a setup has been successfully performed, the application can proceed
|
2024-01-17 18:32:44 +08:00
|
|
|
and use curl_ws_recv(3) and curl_ws_send(3) freely to exchange
|
2023-06-14 16:57:50 +08:00
|
|
|
WebSocket messages with the server.
|
2024-01-17 18:32:44 +08:00
|
|
|
|
|
|
|
# AVAILABILITY
|
|
|
|
|
2023-06-14 16:57:50 +08:00
|
|
|
The WebSocket API was introduced as experimental in 7.86.0 and is still
|
|
|
|
experimental today.
|
|
|
|
|
|
|
|
It is only built-in if explicitly opted in at build time. We discourage use of
|
|
|
|
the WebSocket API in production because of its experimental state. We might
|
|
|
|
change API, ABI and behavior before this "goes live".
|