|
|
@@ -938,38 +938,56 @@ export class RcpSocket {
|
|
|
let lastProgressTime = Date.now();
|
|
|
const progressThrottle = 500; // 进度更新节流,每500ms更新一次
|
|
|
let progressCallCount = 0; // 进度回调计数
|
|
|
-
|
|
|
const customHttpEventsHandler: rcp.HttpEventsHandler = {
|
|
|
- onDataReceive: async (incomingData: ArrayBuffer) => {
|
|
|
- // 上传响应数据接收
|
|
|
- uploadedSize += incomingData.byteLength;
|
|
|
+ onUploadProgress: async (total: number, uploaded: number) => {
|
|
|
+ // 上传进度监控回调
|
|
|
+ uploadedSize = uploaded;
|
|
|
progressCallCount++;
|
|
|
const currentTime = Date.now();
|
|
|
const timeSinceLastUpdate = currentTime - lastProgressTime;
|
|
|
- const isComplete = uploadedSize >= fileSize;
|
|
|
-
|
|
|
+
|
|
|
+ // 更严格的上传完成判断:需要实际传输数据大于文件大小且回调次数足够多
|
|
|
+ const isActuallyTransferring = progressCallCount > 5; // 至少要有几次回调
|
|
|
+ const isComplete = isActuallyTransferring && uploadedSize >= fileSize && uploadedSize >= total;
|
|
|
+
|
|
|
// 节流策略:
|
|
|
// 1. 时间间隔超过阈值
|
|
|
- // 2. 上传完成
|
|
|
- // 3. 每100次回调强制更新一次(防止长时间无更新)
|
|
|
- const shouldUpdate = timeSinceLastUpdate >= progressThrottle ||
|
|
|
- isComplete ||
|
|
|
- (progressCallCount % 100 === 0);
|
|
|
-
|
|
|
+ // 2. 确实上传完成(需要多次回调验证)
|
|
|
+ // 3. 每50次回调强制更新一次(防止长时间无更新)
|
|
|
+ const shouldUpdate = timeSinceLastUpdate >= progressThrottle ||
|
|
|
+ (isActuallyTransferring && isComplete) ||
|
|
|
+ (progressCallCount % 50 === 0);
|
|
|
+
|
|
|
if (onProgress && shouldUpdate) {
|
|
|
- onProgress(uploadedSize, fileSize);
|
|
|
+ // 对于大文件,更加保守地显示进度
|
|
|
+ let displayProgress = uploadedSize;
|
|
|
+ let progressPercentage = Math.floor((uploadedSize / fileSize) * 100);
|
|
|
+
|
|
|
+ // 如果进度超过99%但还没有完成,保守显示99%
|
|
|
+ if (progressPercentage >= 99 && !isComplete) {
|
|
|
+ progressPercentage = 99;
|
|
|
+ displayProgress = Math.min(uploadedSize, fileSize - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ console.info(UtilName, 'testTag', `上传进度更新: ${progressPercentage}% (${displayProgress}/${fileSize} 字节), 回调次数: ${progressCallCount}`);
|
|
|
+ onProgress(displayProgress, fileSize);
|
|
|
lastProgressTime = currentTime;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 后台任务更新也进行节流
|
|
|
if (timeSinceLastUpdate >= 1000) {
|
|
|
await this.backgroundManager.updateDataTransferContinuousTask();
|
|
|
}
|
|
|
},
|
|
|
+ onDataReceive: async (incomingData: ArrayBuffer) => {
|
|
|
+ // 接收服务器响应(这里主要用于监控上传完成后的响应)
|
|
|
+ console.info(UtilName, 'testTag', `接收到响应数据: ${incomingData.byteLength} 字节`);
|
|
|
+ },
|
|
|
onDataEnd: () => {
|
|
|
- console.info(UtilName, 'testTag', '文件数据传输完成');
|
|
|
- // 确保最后一次进度更新
|
|
|
+ console.info(UtilName, 'testTag', '文件数据传输完成,回调次数:', progressCallCount);
|
|
|
+ // 确保最后一次进度更新设置为100%
|
|
|
if (onProgress) {
|
|
|
+ console.info(UtilName, 'testTag', `最终上传完成: ${fileSize} 字节,设置为100%`);
|
|
|
onProgress(fileSize, fileSize);
|
|
|
}
|
|
|
}
|