概述`bufferedAmount` 反映套接字发送缓冲区积压。根据该值动态控制发送速率可减少丢包与断连风险。流控与上传function delay(ms) { return new Promise(r => setTimeout(r, ms)); } async function uploadWithFlow(ws, file, size = 512 * 1024, highWater = 2 * 1024 * 1024) { let idx = 0; for (let o = 0; o < file.size; o += size) { while (ws.bufferedAmount > highWater) await delay(50); const b = file.slice(o, o + size); const ab = await b.arrayBuffer(); const head = new TextEncoder().encode(JSON.stringify({ type: 'chunk', index: idx, name: file.name })); const merged = new Uint8Array(head.length + ab.byteLength); merged.set(head, 0); merged.set(new Uint8Array(ab), head.length); ws.send(merged); idx++; } }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
1.637998s