RTCSctpTransport: thuộc tính maxMessageSize

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2023.

Thuộc tính chỉ đọc maxMessageSize của giao diện RTCSctpTransport cho biết kích thước tối đa của một tin nhắn có thể được gửi bằng phương thức RTCDataChannel.send().

Giá trị

Một giá trị số nguyên cho biết kích thước tối đa, tính bằng byte, của một tin nhắn có thể được gửi bằng phương thức RTCDataChannel.send().

Ví dụ

Ví dụ này cho thấy cách bạn có thể chia chuỗi thành các phần đủ nhỏ để gửi dựa trên kích thước tin nhắn tối đa.

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);
  });
};

Thông số kỹ thuật

Specification
WebRTC: Real-Time Communication in Browsers
# dom-rtcsctptransport-maxmessagesize

Tương thích trình duyệt

Xem thêm