mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Bring the READMEs and the docs pages (en and ja) in line with the current header: - Fix the client sample that does not compile, the Transfer-Encoding statement for default headers, the verifier callback comment, the thread pool spawn rule, the sanitize_filename description, and the inverted Accept-Encoding sentence. - Describe recent behaviour: TLS errors behind Read/Write failures, Host validation, Connection: close for a body without length, content_reader rules, refused CONNECT, WebSocketClient proxies, CPPHTTPLIB_INTERIM_RESPONSE_MAX_COUNT, the SSE event size limit, and the payload limit of the Stream API. - Complete the API references of WebSocket, WebSocketClient and the stream:: helpers, and the SSE reconnect rules. - Fix cookbook pages: keychain loading on every backend, received Close status, a multipart sample that trusted the client's file name, an echo sample that sent binary as text, SNI, pre-request handler scope, stop() with WebSocket handlers, and the thread pool notes. - Restore the mutex in the LLM app's Chapter 5 code.
3.4 KiB
3.4 KiB
title, order, status
| title | order | status |
|---|---|---|
| W03. 接続クローズをハンドリングする | 54 | draft |
WebSocket接続は、クライアントかサーバーのどちらかが明示的に閉じるか、ネットワーク障害で切れると終了します。クローズ処理をきちんと書いておくと、リソースの後始末や再接続ロジックがきれいに書けます。
クローズ状態の検出
ws.read()がReadResult::Failを返したら、接続が切れたか何らかのエラーが起きたということです。ループを抜けてハンドラから戻れば、そのWebSocket接続の処理は終わります。
svr.WebSocket("/chat", [](const httplib::Request &req, httplib::ws::WebSocket &ws) {
std::string msg;
while (ws.is_open()) {
auto result = ws.read(msg);
if (result == httplib::ws::ReadResult::Fail) {
std::cout << "disconnected" << std::endl;
break;
}
handle_message(ws, msg);
}
// ここに到達したら後始末
cleanup_user_session(req);
});
ws.is_open()でも接続状態を確認できます。内部的には同じことを見ています。
サーバー側から閉じる
サーバー側から明示的にクローズしたいときは、close()を呼びます。
ws.close(httplib::ws::CloseStatus::Normal, "bye");
第1引数にクローズステータス、第2引数に理由(任意)を渡します。クローズステータスはCloseStatus列挙値で、代表的なものはこちらです。
| 値 | 意味 |
|---|---|
Normal (1000) |
通常終了 |
GoingAway (1001) |
サーバーが終了するため |
ProtocolError (1002) |
プロトコル違反を検知 |
UnsupportedData (1003) |
対応していないデータを受信 |
PolicyViolation (1008) |
ポリシー違反 |
MessageTooBig (1009) |
メッセージが大きすぎる |
InternalError (1011) |
サーバー内部エラー |
クライアント側から閉じる
クライアント側でも同じAPIが使えます。
cli.close(httplib::ws::CloseStatus::Normal);
cliを破棄したときにも自動的にクローズされますが、明示的にclose()を呼んだほうが意図が伝わりやすいです。
グレースフルシャットダウン
サーバーを停止するときに接続中のクライアントに「これから止まります」と伝えたい場合は、GoingAwayを使います。
ws.close(httplib::ws::CloseStatus::GoingAway, "server restarting");
クライアント側のread()はFailを返すだけで、受け取ったステータスや理由は取り出せません。再起動による切断をほかの切断と見分けたい場合は、先にメッセージ(ws.send("restarting")など)を送ってからclose()を呼びましょう。
サンプル: 簡単なチャット終了
svr.WebSocket("/chat", [](const auto &req, auto &ws) {
std::string msg;
while (ws.is_open()) {
if (ws.read(msg) == httplib::ws::ReadResult::Fail) break;
if (msg == "/quit") {
ws.send("goodbye");
ws.close(httplib::ws::CloseStatus::Normal, "user quit");
break;
}
ws.send("echo: " + msg);
}
});
Note: ネットワーク障害で突然切断された場合、
close()を呼ぶ暇もなくread()がFailを返します。後始末はハンドラ終了時にまとめて行うようにしておくと、どちらのパターンでも対応できます。