RTCSctpTransport:maxMessageSize 屬性
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 |
瀏覽器相容性
載入中…