WebSockets & Realtime Systems Programming · บทเรียน

โพรโทคอลย่อย ส่วนขยาย และการบีบอัด

เจาะลึกโพรโทคอล WebSocket ด้วยการเจรจาโพรโทคอลย่อยของแอปพลิเคชันและเปิดใช้ส่วนขยาย เช่น การบีบอัดแยกต่อข้อความ

บทเรียน 4 จาก 413 ขั้นตอน

โพรโทคอลย่อย ส่วนขยาย และการบีบอัด เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.

เริ่มต้นได้ฟรี

เรียนรู้ WebSockets & Realtime Systems Programming ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
47

คำถามที่พบบ่อย

บทเรียน “โพรโทคอลย่อย ส่วนขยาย และการบีบอัด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โพรโทคอลย่อย ส่วนขยาย และการบีบอัด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โพรโทคอลย่อย ส่วนขยาย และการบีบอัด”

เจาะลึกโพรโทคอล WebSocket ด้วยการเจรจาโพรโทคอลย่อยของแอปพลิเคชันและเปิดใช้ส่วนขยาย เช่น การบีบอัดแยกต่อข้อความ คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “โพรโทคอลย่อย ส่วนขยาย และการบีบอัด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม

ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. อธิบายการจับมือของ WebSocket
  2. การจัดกรอบข้อมูลและข้อความของ WebSocket
  3. วงจรชีวิตและสถานะการเชื่อมต่อ
  4. โพรโทคอลย่อย ส่วนขยาย และการบีบอัด
← กลับไปที่ WebSockets & Realtime Systems Programming