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

瀏覽器相容性

另見