0Pricing
WebSockets & Realtime Systems Programming · 课时

WebSocket 握手过程详解

剖析基于 HTTP 的握手过程,了解它如何将标准 HTTP 连接升级为 WebSocket 连接。

WebSocket 握手过程详解 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebSockets & Realtime Systems Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Handshake: The Connection Upgrade

Imagine you're calling a friend, but instead of just talking, you first agree on a secret language. The WebSocket handshake is similar!

It's the crucial initial step where your browser (client) and a server agree to switch from a standard HTTP connection to a WebSocket connection.

Beyond HTTP's Limitations

Traditional HTTP is designed for short, one-off requests and responses. Think of loading a webpage – you ask, the server sends, and the connection closes.

  • One-way: Client requests, server responds.
  • Short-lived: Connection closes after each interaction.
  • Overhead: Each new request needs to set up a new connection.

WebSockets need a persistent, two-way street for real-time data.

Client Initiates the Upgrade

The handshake begins with the client (your browser) sending a regular HTTP GET request to the server. But it's not truly "regular"!

This request includes special HTTP headers that signal the client's intention to "upgrade" to the WebSocket protocol. It's like adding a special note to your call asking to switch to the secret language.

Signaling the Intent to Switch

Two key headers clearly tell the server the client wants a WebSocket connection:

  • Upgrade: websocket: This header specifies the protocol the client wants to switch to.
  • Connection: Upgrade: This header is a general HTTP header that indicates the client wishes to upgrade or switch protocols.

Together, they form the core of the upgrade request.

The Client's Secret Key

Another vital header from the client is Sec-WebSocket-Key.

  • It's a 16-byte random value, encoded in Base64.
  • It acts as a unique, unpredictable "nonce" (number used once) for each handshake.
  • The server uses this key to prove it understands the WebSocket protocol and to prevent certain types of caching or proxy issues.

Protocol Version Check

The Sec-WebSocket-Version header is straightforward but important.

It tells the server which version of the WebSocket protocol the client is using. Currently, the standard version is 13.

This ensures both client and server speak the same "dialect" of the WebSocket language, guaranteeing compatibility.

Server Responds: 101 Switching Protocols

If the server supports WebSockets and agrees to the upgrade, it sends a special HTTP response back to the client.

The most important part of this response is the HTTP status code 101: Switching Protocols. This code explicitly means the server is changing protocols as requested by the client.

Server's Confirmation Key

The server's response also includes key headers:

  • Upgrade: websocket and Connection: Upgrade: Mirroring the client, these confirm the server's agreement to upgrade.
  • Sec-WebSocket-Accept: This is a hash generated by the server using the client's Sec-WebSocket-Key plus a specific "magic string". It proves the server understood the key and is capable of WebSockets.

Handshake Key Check

You've learned about the important headers involved in the WebSocket handshake.

Which of the following headers is sent by the client to initiate the WebSocket upgrade, and contains a random, unique value?

Connection Upgraded & Ready!

Once the client receives and validates the server's 101 response with the correct Sec-WebSocket-Accept header, the HTTP handshake is complete!

The connection is now upgraded to a full-duplex WebSocket. Both client and server can send and receive data simultaneously over this persistent connection, enabling true real-time communication.

常见问题解答

「WebSocket 握手过程详解」课时是免费的吗?

是的 — 「WebSocket 握手过程详解」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 4 节课。

「WebSocket 握手过程详解」这节课中我会学到什么?

剖析基于 HTTP 的握手过程,了解它如何将标准 HTTP 连接升级为 WebSocket 连接。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebSockets & Realtime Systems Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「WebSocket 握手过程详解」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?

能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. WebSocket 握手过程详解
  2. WebSocket 数据分帧与消息
  3. 连接生命周期与状态
  4. 子协议、扩展与压缩
← 返回 WebSockets & Realtime Systems Programming