大网数据广告活动
Web 标准与前端开发

RTCDataChannel: label property

· 52CE 我爱测

The read-only `RTCDataChannel` property **`label`** returns a string containing a name describing the data channel. These labels are not required to be unique.

WebRTC

The read-only RTCDataChannel property label returns a string containing a name describing the data channel. These labels are not required to be unique.

You may use the label as you wish; you could use it to identify all the channels that are being used for the same purpose, by giving them all the same name. Or you could give each channel a unique label for tracking purposes. It's entirely up to the design decisions made when building your site or app.

A unique ID can be found in the RTCDataChannel.id property.

[!NOTE] A data channel's label is set when the channel is created by calling RTCPeerConnection.createDataChannel(). It cannot be changed after that.

Value

A string identifier assigned by the website or app when the data channel was created, as specified when RTCPeerConnection.createDataChannel() was called to create the channel.

Example

This sample creates a data channel on an RTCPeerConnection, then, some time later, sets the content of a UI element to display the channel's name.

const pc = new RTCPeerConnection();
const dc = pc.createDataChannel("my channel");

// …

document.getElementById("channel-name").appendChild(
  Object.assign(document.createElement("span"), {
    className: "channelName",
    textContent: dc.label,
  }),
);

Specifications

Browser compatibility

See also

  • WebRTC
  • RTCDataChannel
  • RTCDataChannel.id
  • RTCPeerConnection.createDataChannel()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。