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

ตัวเลือก ICE และการเชื่อมต่อ

สำรวจตัวเลือก ICE ว่าใช้แทนที่อยู่เครือข่ายอย่างไร และ ICE ช่วยค้นหาเส้นทางที่ดีที่สุดสำหรับการเชื่อมต่อระหว่างเพียร์อย่างไร

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

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

ICE: The Connectivity Finder

ICE stands for Interactive Connectivity Establishment. It's a crucial framework in WebRTC that helps establish direct connections between peers, even when they are behind tricky networks like NATs or firewalls.

Think of ICE as a smart detective. It finds all possible routes for two people to talk directly and then picks the best one.

Overcoming Network Barriers

Many devices connect to the internet through routers that use NAT (Network Address Translation) or have firewalls. These act like security guards, often blocking direct incoming connections.

  • NAT: Hides private network IPs behind a single public IP.
  • Firewalls: Block unauthorized access.

Without ICE, establishing a direct peer-to-peer connection in such environments would be nearly impossible.

What Are ICE Candidates?

An ICE Candidate is essentially a potential network address and port where a peer can be reached. Each peer collects multiple candidates representing different ways it can communicate.

These candidates are like different phone numbers or addresses you might have: your home number, your work number, a friend's number where you can be reached, etc. ICE tries them all.

Types of ICE Candidates

There are three main types of ICE candidates:

  • Host Candidates: These are the peer's actual local IP addresses. They work best for direct connections within the same local network.
  • Server Reflexive Candidates: Obtained from a STUN server. This is your public IP address and port as seen by an external server, helping peers behind NATs find each other.
  • Relayed Candidates: Obtained from a TURN server. If direct connection isn't possible, a TURN server relays all traffic. This is the last resort.

Gathering Candidates: The Process

When a WebRTC connection is initiated, each peer's browser (the WebRTC agent) starts collecting ICE candidates. It actively queries the local network, STUN servers, and potentially TURN servers to find all possible communication paths.

This collection process happens continuously in the background as the RTCPeerConnection is being set up.

Code: Listening for Candidates

In JavaScript, you listen for icecandidate events on your RTCPeerConnection object to get these candidates. Each event provides a new candidate to share with the remote peer via your signaling server.

const pc = new RTCPeerConnection();

pc.onicecandidate = (event) => {
  if (event.candidate) {
    console.log("New ICE candidate found:");
    console.log(event.candidate.candidate);
    // Send this candidate to the remote peer via signaling server
  } else {
    console.log("ICE candidate gathering complete.");
  }
};

console.log("Listening for ICE candidates...");
// Note: This snippet requires a full browser WebRTC context to run
// and produce actual candidates. It's for demonstration.

Exchanging Candidates: Signaling

Once candidates are gathered, they must be exchanged between the two peers. This happens through your signaling server, the same server used to exchange SDP offers and answers.

  • Each peer sends its collected candidates to the signaling server.
  • The signaling server forwards these candidates to the other peer.

This process is often called "trickle ICE" because candidates are sent as they are found, rather than waiting for all of them.

The Connectivity Check

After exchanging candidates, ICE begins its connectivity checks. Both peers try to establish connections using every possible pair of local and remote candidates. This involves sending small "STUN binding requests" to test reachability.

ICE then prioritizes and selects the most efficient and reliable path. It prefers direct host connections, then STUN-relayed public IPs, and finally TURN-relayed connections.

Quick Check: ICE Candidate Types

Which type of ICE candidate is obtained with the help of a STUN server to reveal a peer's public IP address?

Recap: ICE in Action

In this lesson, you learned about ICE Candidates and how they enable WebRTC to establish peer-to-peer connections across diverse networks.

  • ICE candidates are potential network addresses.
  • They come in types: Host, Server Reflexive (STUN), and Relayed (TURN).
  • Peers gather and exchange these candidates via a signaling server.
  • ICE then performs connectivity checks to find the best possible path for direct communication.

ICE is the unsung hero ensuring your real-time calls find a way through the internet's complexities!

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

บทเรียน “ตัวเลือก ICE และการเชื่อมต่อ” ฟรีหรือไม่

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

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

สำรวจตัวเลือก ICE ว่าใช้แทนที่อยู่เครือข่ายอย่างไร และ ICE ช่วยค้นหาเส้นทางที่ดีที่สุดสำหรับการเชื่อมต่อระหว่างเพียร์อย่างไร คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

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

บทเรียน 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)