Real-Time Streaming Systems (WebRTC + Live Data) · บทเรียน

การเจรจาใหม่และสถานะการเชื่อมต่อ

เรียนรู้ว่าการเชื่อมต่อระหว่างเพียร์ของ WebRTC เปลี่ยนแปลงตามเวลาอย่างไร ตั้งแต่การเฝ้าติดตามสถานะการเชื่อมต่อ การจัดการการเริ่ม ICE ใหม่ ไปจนถึงการเจรจา SDP ใหม่เมื่อสื่อหรือเงื่อนไขเครือข่ายเปลี่ยนแปลง

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

การเจรจาใหม่และสถานะการเชื่อมต่อ เป็นบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Real-Time Streaming Systems (WebRTC + Live Data) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Real-Time Streaming Systems (WebRTC + Live Data) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Connections Are Not Static

You have set up signaling, SDP, and ICE to create a connection. But a real session evolves: a user adds screen share, switches networks, or recovers from a drop. Handling these changes is called renegotiation and state management.

The Connection State Machine

An RTCPeerConnection reports its health through connectionState, moving through values like new, connecting, connected, disconnected, failed, and closed.

pc.onconnectionstatechange = () => {
  console.log('state:', pc.connectionState);
};

Reacting to State

Use the state to drive UI and recovery logic, such as showing a reconnecting spinner or tearing down a dead connection.

pc.onconnectionstatechange = () => {
  switch (pc.connectionState) {
    case 'connected': showCall(); break;
    case 'disconnected': showReconnecting(); break;
    case 'failed': restartConnection(); break;
    case 'closed': cleanup(); break;
  }
};

ICE Connection State

Separately, iceConnectionState tracks the connectivity checks. A transition to disconnected may be temporary, while failed usually means connectivity must be re-established.

pc.oniceconnectionstatechange = () => {
  if (pc.iceConnectionState === 'failed') {
    pc.restartIce();
  }
};

What Is an ICE Restart?

When a network path dies (for example, switching from Wi-Fi to cellular), the existing candidates no longer work. An ICE restart gathers fresh candidates and finds a new path without recreating the whole connection.

When Renegotiation Is Needed

Any change to the set of media tracks or transceivers requires a new offer/answer exchange. The browser tells you via the negotiationneeded event.

pc.onnegotiationneeded = async () => {
  const offer = await pc.createOffer();
  await pc.setLocalDescription(offer);
  signaling.send({ type: 'offer', sdp: offer });
};

Adding a Track Mid-Call

Adding a screen-share track during a call triggers negotiationneeded, prompting a fresh SDP exchange that updates the remote peer.

async function startScreenShare() {
  const screen = await navigator.mediaDevices.getDisplayMedia();
  const track = screen.getVideoTracks()[0];
  pc.addTrack(track, screen); // fires negotiationneeded
}

The Glare Problem

If both peers create offers at the same time, they collide. This is called glare. The fix is the perfect negotiation pattern, where one peer is polite and rolls back its offer when a collision occurs.

Perfect Negotiation Sketch

A polite peer rolls back on collision; the impolite peer ignores the incoming offer. This guarantees exactly one negotiation succeeds.

const offerCollision = (msg.type === 'offer') &&
  (makingOffer || pc.signalingState !== 'stable');
ignoreOffer = !polite && offerCollision;
if (ignoreOffer) return;
if (offerCollision) await pc.setLocalDescription({ type: 'rollback' });

Closing Cleanly

When a call ends, stop tracks and close the connection to free resources and release the camera and microphone.

function hangUp() {
  pc.getSenders().forEach(s => s.track && s.track.stop());
  pc.close();
}

Putting It Together

Robust WebRTC apps monitor connection state, restart ICE on path loss, renegotiate when tracks change, and handle glare with perfect negotiation. These behaviors turn a fragile demo into a reliable product.

Quick Check

Test your understanding of renegotiation.

Recap

You learned about connection state and renegotiation:

  • connectionState and iceConnectionState report health
  • ICE restart recovers from network path changes
  • negotiationneeded triggers a fresh SDP exchange
  • Perfect negotiation resolves glare; clean shutdown frees devices

These skills keep peer connections resilient over time.

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

เรียนรู้ Real-Time Streaming Systems (WebRTC + Live Data) ด้วย AI tutor — ฟรี

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

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

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

บทเรียน “การเจรจาใหม่และสถานะการเชื่อมต่อ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเจรจาใหม่และสถานะการเชื่อมต่อ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Real-Time Streaming Systems (WebRTC + Live Data) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Real-Time Streaming Systems (WebRTC + Live Data) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเจรจาใหม่และสถานะการเชื่อมต่อ”

เรียนรู้ว่าการเชื่อมต่อระหว่างเพียร์ของ WebRTC เปลี่ยนแปลงตามเวลาอย่างไร ตั้งแต่การเฝ้าติดตามสถานะการเชื่อมต่อ การจัดการการเริ่ม ICE ใหม่ ไปจนถึงการเจรจา SDP ใหม่เมื่อสื่อหรือเงื่อนไขเครือข่ายเปลี่ยน… คุณปฏิบัติ Real-Time Streaming Systems (WebRTC + Live Data) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Real-Time Streaming Systems (WebRTC + Live Data) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Real-Time Streaming Systems (WebRTC + Live Data) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเจรจาใหม่และสถานะการเชื่อมต่อ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) นี้ได้ไหม

ได้ บทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. บทบาทของเซิร์ฟเวอร์ส่งสัญญาณ
  2. SDP: โพรโทคอลคำอธิบายเซสชัน
  3. ตัวเลือก ICE และการเชื่อมต่อ
  4. การเจรจาใหม่และสถานะการเชื่อมต่อ
← กลับไปที่ Real-Time Streaming Systems (WebRTC + Live Data)