mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-22 18:07:16 +00:00
T04 (mTLS) had grown a "WebSocketClient" subsection describing wss:// client certificates, and c12/t02 were getting similar WebSocketClient asides for timeouts and CA paths. The Cookbook's own index already separates WebSocket into its own category (W01-W04) from TLS/Security (T01-T05) and Client (C01-C19), so burying WebSocketClient specifics inside those pages fought the site's structure. Move that content into two new recipes under the WebSocket category instead: - W05: wss:// TLS setup (set_ca_cert_path CA directory parity, PemMemory client certificate) - W06: WebSocketClient's three timeouts, including the recently added chrono overloads T04, T02, C12, and W01 now carry a single reference link to the new pages instead of duplicated explanations, matching the site's existing cross-link convention. While rewriting T04's client-side section, noticed it documented SSLClient's file-path constructor but not its PemMemory one, even though the server-side section covered both forms for SSLServer. Added the missing PemMemory example so both sides are symmetric.
53 lines
1.8 KiB
Markdown
53 lines
1.8 KiB
Markdown
---
|
|
title: "C12. Set Timeouts"
|
|
order: 12
|
|
status: "draft"
|
|
---
|
|
|
|
The client has three kinds of timeouts, each set independently.
|
|
|
|
| Kind | API | Default | Meaning |
|
|
| --- | --- | --- | --- |
|
|
| Connection | `set_connection_timeout` | 300s | Time to wait for the TCP connection to establish |
|
|
| Read | `set_read_timeout` | 300s | Time to wait for a single `recv` when receiving the response |
|
|
| Write | `set_write_timeout` | 5s | Time to wait for a single `send` when sending the request |
|
|
|
|
## Basic usage
|
|
|
|
```cpp
|
|
httplib::Client cli("http://localhost:8080");
|
|
|
|
cli.set_connection_timeout(5, 0); // 5 seconds
|
|
cli.set_read_timeout(10, 0); // 10 seconds
|
|
cli.set_write_timeout(10, 0); // 10 seconds
|
|
|
|
auto res = cli.Get("/api/data");
|
|
```
|
|
|
|
Pass seconds and microseconds as two arguments. If you don't need the sub-second part, you can omit the second argument.
|
|
|
|
## Use `std::chrono`
|
|
|
|
There's also an overload that takes a `std::chrono` duration directly. It's easier to read — recommended.
|
|
|
|
```cpp
|
|
using namespace std::chrono_literals;
|
|
|
|
cli.set_connection_timeout(5s);
|
|
cli.set_read_timeout(10s);
|
|
cli.set_write_timeout(500ms);
|
|
```
|
|
|
|
## Watch out for the long 300s default
|
|
|
|
Connection and read timeouts default to **300 seconds (5 minutes)**. If the server hangs, you'll be waiting five minutes by default. Shorter values are usually a better idea.
|
|
|
|
```cpp
|
|
cli.set_connection_timeout(3s);
|
|
cli.set_read_timeout(10s);
|
|
```
|
|
|
|
> **Warning:** The read timeout covers a single receive call — not the whole request. If data keeps trickling in during a large download, the request can take half an hour without ever hitting the timeout. To cap the total request time, use [C13. Set an overall timeout](../c13-max-timeout).
|
|
|
|
> For WebSocket client timeouts, see [W06. Set Timeouts](../w06-websocket-timeouts).
|