可靠与不可靠的数据通道
学习如何配置 RTCDataChannel 的可靠性和顺序,了解何时为游戏选择不可靠模式,以及如何安全处理背压和二进制数据。
可靠与不可靠的数据通道 是 CoddyKit 上的免费 Real-Time Streaming Systems (WebRTC + Live Data) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Real-Time Streaming Systems (WebRTC + Live Data) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Channels Are Configurable
You can already send and receive data over an RTCDataChannel. Now you will tune its behavior. Unlike a fixed protocol, data channels let you choose between reliable and unreliable delivery, just like TCP versus UDP.
Reliable Ordered Default
By default a data channel is reliable and ordered: every message arrives, in the sequence it was sent. This is perfect for chat or file transfer where correctness matters most.
const chat = pc.createDataChannel('chat');
// reliable + ordered by defaultThe SCTP Foundation
Data channels run over SCTP, a protocol that supports both reliable and partially reliable modes. This flexibility is why you can trade reliability for lower latency.
Unordered Delivery
Set ordered: false to let messages arrive in any order. Useful when each message is independent and waiting for order would add delay.
const dc = pc.createDataChannel('positions', {
ordered: false
});Limited Retransmits
Use maxRetransmits to cap how many times a lost message is resent. Setting it to 0 means fire-and-forget, ideal for fast-changing game state.
const dc = pc.createDataChannel('game', {
ordered: false,
maxRetransmits: 0
});Time-Limited Reliability
Alternatively, maxPacketLifeTime retries a message only for a set number of milliseconds, then gives up. Use this instead of (not together with) maxRetransmits.
const dc = pc.createDataChannel('telemetry', {
ordered: false,
maxPacketLifeTime: 1000
});Choosing a Mode
Match the mode to the use case:
- Reliable ordered: chat, files, commands
- Unordered, no retransmit: player positions, sensor data
- Time-limited: data only useful briefly
Sending Binary Data
Channels can carry binary as ArrayBuffer or typed arrays, not just strings. Set binaryType on the receiver to control how it is delivered.
dc.binaryType = 'arraybuffer';
dc.send(new Uint8Array([1, 2, 3, 4]).buffer);Backpressure
Sending too fast fills the outgoing buffer. Check bufferedAmount and pause when it grows beyond a threshold to avoid overwhelming the connection.
const LIMIT = 65535;
function safeSend(dc, data) {
if (dc.bufferedAmount < LIMIT) {
dc.send(data);
return true;
}
return false; // wait and retry later
}Draining the Buffer
Listen for bufferedamountlow to resume sending once the buffer drains, creating a smooth flow-controlled pipeline.
dc.bufferedAmountLowThreshold = 16384;
dc.onbufferedamountlow = () => {
resumeSending();
};Putting It Together
Configure reliability and ordering to fit your data, use binary types for efficiency, and respect backpressure so the channel stays responsive. These choices make data channels powerful for games, file sharing, and live collaboration.
Quick Check
Test your understanding of channel configuration.
Recap
You learned to configure data channel reliability:
- Reliable ordered by default, over SCTP
ordered:false,maxRetransmits, andmaxPacketLifeTimetrade reliability for latency- Binary data via
binaryType - Backpressure with
bufferedAmountandbufferedamountlow
Tune each channel to its data's needs.
用 AI 导师学习 Real-Time Streaming Systems (WebRTC + Live Data) — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「可靠与不可靠的数据通道」课时是免费的吗?
是的 — 「可靠与不可靠的数据通道」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Real-Time Streaming Systems (WebRTC + Live Data) 课程的其余内容,请升级到 CoddyKit PRO。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。
「可靠与不可靠的数据通道」这节课中我会学到什么?
学习如何配置 RTCDataChannel 的可靠性和顺序,了解何时为游戏选择不可靠模式,以及如何安全处理背压和二进制数据。 你通过在浏览器中直接运行的动手代码来练习 Real-Time Streaming Systems (WebRTC + Live Data),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Real-Time Streaming Systems (WebRTC + Live Data) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Real-Time Streaming Systems (WebRTC + Live Data) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「可靠与不可靠的数据通道」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Real-Time Streaming Systems (WebRTC + Live Data) 课中编写并运行代码吗?
能。每节 Real-Time Streaming Systems (WebRTC + Live Data) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- RTCDataChannel 简介
- 发送和接收数据
- 数据通道的实际应用场景
- 可靠与不可靠的数据通道