0Pricing
WebSockets & Realtime Systems Programming · 课时

子协议、扩展与压缩

深入了解 WebSocket 协议:协商应用子协议,并启用按消息压缩等扩展。

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

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

Beyond the Basics

The raw WebSocket protocol only moves bytes. Two negotiation mechanisms let you layer meaning and efficiency on top: subprotocols and extensions.

What Is a Subprotocol

A subprotocol is an agreed application-level message format, like a contract for what the bytes mean. Examples include wamp, mqtt, and graphql-ws.

Requesting a Subprotocol

The client lists desired subprotocols in a handshake header.

Sec-WebSocket-Protocol: graphql-ws, json

Server Selection

The server picks one subprotocol it supports and echoes it back. If none match, it omits the header and only the base protocol applies.

Sec-WebSocket-Protocol: graphql-ws

Subprotocols in the Browser

Pass an array of subprotocol names as the second argument when opening the socket.

const ws = new WebSocket('wss://api.example.com', ['graphql-ws', 'json']);
console.log(ws.protocol); // the one the server chose

What Is an Extension

An extension changes how frames are transmitted at the protocol level, transparent to your application logic. The most common is per-message compression.

Negotiating Extensions

Extensions are negotiated in the handshake too, via their own header.

Sec-WebSocket-Extensions: permessage-deflate

Per-Message Deflate

permessage-deflate compresses each message with DEFLATE before sending. It cuts bandwidth for text-heavy payloads at the cost of CPU.

When Compression Helps

Compression wins for large, repetitive JSON or text. For tiny or already-compressed payloads (images, binary), it adds overhead without benefit and can be disabled.

Subprotocol vs Extension

Keep them straight:

  • Subprotocol: meaning of the messages (application layer)
  • Extension: how frames are transported (protocol layer)

Practical Notes

Both are negotiated only during the handshake and cannot change afterward. Always verify ws.protocol client-side so your code parses messages with the agreed format.

Quick Check

Test your protocol knowledge.

Recap

You explored advanced WebSocket negotiation:

  • Subprotocols agree on message format via Sec-WebSocket-Protocol
  • Extensions change frame transport via Sec-WebSocket-Extensions
  • permessage-deflate compresses text payloads
  • Both are fixed at handshake time

You now understand the negotiation layer of WebSockets.

常见问题解答

「子协议、扩展与压缩」课时是免费的吗?

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

「子协议、扩展与压缩」这节课中我会学到什么?

深入了解 WebSocket 协议:协商应用子协议,并启用按消息压缩等扩展。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「子协议、扩展与压缩」课时需要多长时间?

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

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

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

此课程中的所有课时

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