From 4db96a640482fccd9ae52aae7e1ddf9eb3f023c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Sat, 11 Jul 2026 00:26:03 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20WebSocket=20flow=20control=20=E2=80=94?= =?UTF-8?q?=20bufferedAmount=20+=20threshold?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- site/static/app.js | 54 ++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/site/static/app.js b/site/static/app.js index 0dd6210..2d17533 100644 --- a/site/static/app.js +++ b/site/static/app.js @@ -209,31 +209,43 @@ function uploadFileWS(url, f, idx) { ws.binaryType = 'arraybuffer'; ws.onopen = function () { + ws.bufferedAmountLowThreshold = CHUNK; var reader = new FileReader(); - reader.onload = function (e) { - ws.send(e.target.result); - offset += CHUNK; - if (offset < f.size) { - var pct = Math.round(offset / f.size * 100); - if (pct - lastProgress >= 10) { - lastProgress = pct; - f.progress = pct; - renderTable(); - } - readNext(); - } else { - // Последний чанк + сигнал DONE - ws.send(e.target.result); - ws.send('DONE'); - f.progress = 100; - renderTable(); - } - }; - function readNext() { + + function sendNext() { var end = Math.min(offset + CHUNK, f.size); reader.readAsArrayBuffer(f.file.slice(offset, end)); } - readNext(); + + reader.onload = function (e) { + ws.send(e.target.result); + offset += CHUNK; + + var pct = Math.round(offset / f.size * 100); + if (pct - lastProgress >= 5) { + lastProgress = pct; + f.progress = pct; + renderTable(); + } + + if (offset >= f.size) { + // Когда буфер опустеет — шлём DONE + function checkDone() { + if (ws.bufferedAmount === 0) { + ws.send('DONE'); + } else { + setTimeout(checkDone, 50); + } + } + checkDone(); + } + }; + + ws.addEventListener('bufferedamountlow', function () { + if (offset < f.size) sendNext(); + }); + + sendNext(); }; ws.onmessage = function (e) {