ADataChannel
有一个bufferedAmount
属性,它告诉您仍有多少字节等待发送。它还有一个名为bufferedAmountLowThreshold
.
RTCDataChannel 属性 bufferedAmountLowThreshold 用于指定被视为“低”的缓冲传出数据的字节数。
https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/bufferedAmountLowThreshold
https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/bufferedAmount
只要在bufferedAmount
下面,您就可以继续正常发送数据bufferedAmountLowThreshold
。一旦它变大,您将停止排队更多数据,直到您收到bufferedamountlow
事件。
const send = () => {
while (buffer.byteLength) {
if (dataChannel.bufferedAmount > dataChannel.bufferedAmountLowThreshold) {
dataChannel.onbufferedamountlow = () => {
dataChannel.onbufferedamountlow = null;
send();
};
return;
}
const chunk = buffer.slice(0, chunkSize);
buffer = buffer.slice(chunkSize, buffer.byteLength);
dataChannel.send(chunk);
}
};
send();