我正在尝试通过 WebRTC 共享文件,但一段时间后它会停止,并且记录 RTCDatachannel 发送队列已满

发布于 2025-01-10 17:50:24 字数 373 浏览 0 评论 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);
  }
})

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

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

it works fine for small files but stops with big size files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2025-01-17 17:50:24

DataChannel 有一个 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();

A DataChannel has a bufferedAmount property which tells you how many bytes are still waiting to be sent. It also has a property called bufferedAmountLowThreshold.

The RTCDataChannel property bufferedAmountLowThreshold is used to specify the number of bytes of buffered outgoing data that is considered "low."

https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/bufferedAmountLowThreshold
https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/bufferedAmount

You could keep sending data as normal as long as bufferedAmount is below bufferedAmountLowThreshold. Once it is larger you stop queuing more data until you receive a bufferedamountlow event.

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文