RTCSctpTransport:maxMessageSize 属性

Baseline 2023
新推出

自 ⁨2023 年 5 月⁩起,此功能可在最新的设备和浏览器版本上使用。此功能可能无法在旧版设备或浏览器上使用。

RTCSctpTransport 接口的只读属性 maxMessageSize 指示使用 RTCDataChannel.send() 方法可以发送的消息的最大尺寸。

一个整数值,表示使用 RTCDataChannel.send() 方法可以发送的消息的最大尺寸(以字节为单位)。

示例

此示例展示了如何根据最大消息大小将字符串拆分为足够小的部分进行发送。

js
// Function splits strings to a specified size and returns array.
function splitStringToMax(str, maxLength) {
  const result = [];
  let i = 0;
  while (i < str.length) {
    result.push(str.substring(i, i + maxLength));
    i += maxLength;
  }
  return result;
}

const peerConnection = new RTCPeerConnection(options);
const channel = peerConnection.createDataChannel("chat");
channel.onopen = (event) => {
  const maximumMessageSize = peerConnection.sctp.maxMessageSize;
  const textToSend = "This is my possibly overly long string!";
  splitStringToMax(textToSend, maximumMessageSize).forEach((elem) => {
    channel.send(elem);
  });
};

规范

规范
WebRTC:浏览器中的实时通信
# dom-rtcsctptransport-maxmessagesize

浏览器兼容性

另见