0
let file = fileUpload.files[0];
let offset = 0;
let chunkSize = 1024*1024*16;


file.arrayBuffer().then((buffer) => {
            
 while(buffer.byteLength){
                
       const chunk = buffer.slice(0, chunkSize);
       buffer = buffer.slice(chunkSize, buffer.byteLength);
      dataChannel.send(chunk);
  }
})

它适用于小文件,但适用于大文件。

4

1 回答 1

0

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();
于 2022-02-27T19:14:07.970 回答