RFC 6455 - The WebSocket Protocol (WebSocketプロトコル)
発行日: 2011年12月
ステータス: 標準化過程 (Standards Track)
著者: I. Fette (Google Inc.), A. Melnikov (Isode Ltd.)
概要 (Abstract)
WebSocketプロトコル (WebSocket Protocol) は、単一のTCP接続上でクライアントとサーバー間の全二重通信 (Full-Duplex Communication) を実現します。WebSocketプロトコルは、Webブラウザおよびwebサーバーに実装されることを想定していますが、任意のクライアントまたはサーバーアプリケーションで使用できます。
WebSocketプロトコルは、TCPベースの独立したプロトコルです。HTTPとの唯一の関係は、そのハンドシェイク (Handshake) がHTTPサーバーによってアップグレードリクエスト (Upgrade Request) として解釈されることです。
目次 (Table of Contents)
- 1. Introduction (はじめに)
- 1.1 Background (背景)
- 1.2 Protocol Overview (プロトコル概要)
- 1.3 Opening Handshake (オープニングハンドシェイク)
- 1.4 Closing Handshake (クロージングハンドシェイク)
- 1.5 Design Philosophy (設計思想)
- 1.6 Security Model (セキュリティモデル)
- 1.7 Relationship to TCP and HTTP (TCPおよびHTTPとの関係)
- 1.8 Establishing a Connection (接続の確立)
- 1.9 Subprotocols and Extensions (サブプロトコルと拡張)
- 2. Conformance Requirements (適合性要件)
- 2.1 Terminology and Other Conventions (用語とその他の規約)
- 3. WebSocket URIs
- 4. Opening Handshake (オープニングハンドシェイク)
- 4.1 Client Requirements (クライアント要件)
- 4.2 Server-Side Requirements (サーバー側要件)
- 4.3 Collecting Extensions and Subprotocols (拡張とサブプロトコルの収集)
- 4.4 Supporting Multiple Versions (複数バージョンのサポート)
- 5. Data Framing (データフレーミング)
- 5.1 Overview (概要)
- 5.2 Base Framing Protocol (基本フレーミングプロトコル)
- 5.3 Client-to-Server Masking (クライアントからサーバーへのマスキング)
- 5.4 Fragmentation (フラグメンテーション)
- 5.5 Control Frames (制御フレーム)
- 5.6 Data Frames (データフレーム)
- 5.7 Examples (例)
- 5.8 Extensibility (拡張性)
- 6. Sending and Receiving Data (データの送受信)
- 6.1 Sending Data (データの送信)
- 6.2 Receiving Data (データの受信)
- 7. Closing the Connection (接続のクローズ)
- 7.1 Definitions (定義)
- 7.2 Abnormal Closures (異常クローズ)
- 7.3 Normal Closure (正常クローズ)
- 7.4 Status Codes (ステータスコード)
- 8. Error Handling (エラー処理)
- 8.1 Handling Errors in UTF-8-Encoded Data (UTF-8エンコードデータのエラー処理)
- 9. Extensions (拡張)
- 9.1 Negotiating Extensions (拡張のネゴシエーション)
- 9.2 Known Extensions (既知の拡張)
- 10. Security Considerations (セキュリティに関する考慮事項)
- 10.1 Non-Browser Clients (非ブラウザクライアント)
- 10.2 Origin Considerations (オリジンに関する考慮事項)
- 10.3 Attacks On Infrastructure (インフラストラクチャへの攻撃)
- 10.4 Implementation-Specific Limits (実装固有の制限)
- 10.5 WebSocket Client Authentication (WebSocketクライアント認証)
- 10.6 Connection Confidentiality and Integrity (接続の機密性と完全性)
- 10.7 Handling of Invalid Data (無効なデータの処理)
- 10.8 Use of SHA-1 (SHA-1の使用)
- 11. IANA Considerations (IANAに関する考慮事項)
- 12. Using WebSocket Protocol from Other Specifications (他の仕様からのWebSocketプロトコルの使用)
- 13. Acknowledgements (謝辞)
- 14. References (参考文献)
- 14.1 Normative References (規範的参考文献)
- 14.2 Informative References (参考情報)
追加リソース
- 📖 実装ガイド: WebSocketプロトコル実装ガイド - 詳細な技術説明、コード例、ベストプラクティス
- 🔗 公式RFC: RFC 6455
- 📋 DataTracker: RFC 6455 DataTracker
- 🐛 正誤表: RFC Editor Errata
関連RFC
- RFC 6455: The WebSocket Protocol ← 本文書
- RFC 7692: WebSocket Compression Extensions (WebSocket圧縮拡張)
- RFC 8441: Bootstrapping WebSockets with HTTP/2 (HTTP/2でのWebSocketブートストラップ)
クイックリファレンス
WebSocket URI形式
ws-URI = "ws:" "//" host [ ":" port ] path [ "?" query ]
wss-URI = "wss:" "//" host [ ":" port ] path [ "?" query ]
ws://- 暗号化されていない接続 (デフォルトポート80)wss://- TLS暗号化接続 (デフォルトポート443、本番環境推奨)
ハンドシェイク例
クライアントリクエスト:
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Origin: http://example.com
サーバーレスポンス:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
フレームタイプ (Opcode)
| Opcode | タイプ | 説明 |
|---|---|---|
| 0x0 | Continuation | 継続フレーム |
| 0x1 | Text | テキストフレーム (UTF-8) |
| 0x2 | Binary | バイナリフレーム |
| 0x8 | Close | クローズフレーム |
| 0x9 | Ping | Pingフレーム (ハートビート) |
| 0xA | Pong | Pongフレーム (ハートビート応答) |
クローズステータスコード
| コード | 名称 | 説明 |
|---|---|---|
| 1000 | Normal Closure | 正常クローズ |
| 1001 | Going Away | エンドポイントの離脱 (例: ページナビゲーション) |
| 1002 | Protocol Error | プロトコルエラー |
| 1003 | Unsupported Data | サポートされていないデータ型 |
| 1006 | Abnormal Closure | 異常クローズ (Closeフレーム未送信) |
| 1009 | Message Too Big | メッセージが大きすぎる |
| 1011 | Internal Error | サーバー内部エラー |
フレーム構造 (基本形式)
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+-------------------------------+
| Extended payload length continued, if payload len == 127 |
+-------------------------------+-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------+-------------------------------+
重要ポイント:
- FIN: 1=最終フレーム, 0=後続フレームあり
- MASK: クライアント→サーバーは1でなければならない、サーバー→クライアントは0でなければならない
- Opcode: フレームタイプ識別子
- Payload Length: データ長 (0-125は直接表現、126は16ビット使用、127は64ビット使用)
主要用語
| 英語用語 | 日本語訳 | 説明 |
|---|---|---|
| Full-Duplex Communication | 全二重通信 | 双方向同時通信 |
| Opening Handshake | オープニングハンドシェイク | HTTPからWebSocketへのアップグレードプロセス |
| Closing Handshake | クロージングハンドシェイク | 正常な接続クローズプロセス |
| Frame | フレーム | データ転送の基本単位 |
| Message | メッセージ | 1つ以上のフレームで構成 |
| Masking | マスキング | クライアントデータは必須マスク (セキュリティ機構) |
| Fragmentation | フラグメンテーション | 大きなメッセージを複数フレームで転送 |