記錄一個multipart/x-mixed-replace; boundary=frame相應頭的問題
- 需求背景
實現(xiàn)使用img接收流模擬一個實時播放錄像的功能,手動控制src屬性,碰到一個問題,后端返回的是連續(xù)的幀,用img標簽接url,我第一次接沒問題,時間到了切換成靜態(tài)url封面,等我第二次接的時候就報錯,穩(wěn)定奇數(shù)次數(shù)沒問題,偶數(shù)報錯。
- 問題代碼
//開始讀取 const handleStart = debounce(() => { if (!isPlaying) { setIsPlaying(true); setTimeLeft(maxReadTime); setImgUrl(`${streamUrl}${streamUrl.includes("?") ? "&" : "?"}max_read_time=${maxReadTime}&_t=${Date.now()}`); } }, 500); //停止 const stopStream = () => { setIsPlaying(false); if (imgRef.current) { imgRef.current.onerror = null; // ?? 移除舊監(jiān)聽,避免觸發(fā) SyntheticBaseEvent imgRef.current.src = ""; // 主動斷流 } setTimeout(() => { if (isMountedRef.current) { setImgUrl(dkePoster); } }, 100); //定時器相關(guān) useEffect(() => { if (!isPlaying) return; const timer = setInterval(() => { setTimeLeft((t) => { if (t <= 1) { clearInterval(timer); stopStream(); return 0; } return t - 1; }); }, 1000); return () => clearInterval(timer); }, [isPlaying]);
- 解決方案
這里的問題出在定時器會提前一秒結(jié)束,所以修改判斷條件
t <= 1 為 t == 0
但是還是有個疑問待解決,為什么瀏覽器會報出net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK),這個暫時沒有找到相關(guān)資料

浙公網(wǎng)安備 33010602011771號