fix: SSE disconnect sometimes

This commit is contained in:
Tristan Yang
2022-12-06 20:58:24 +08:00
parent 81e7b11fc0
commit 965b738410
+6 -5
View File
@@ -84,17 +84,18 @@ export default function useStreaming() {
}; };
SSE.onerror = (err) => { SSE.onerror = (err) => {
const { readyState } = err.target as EventSource; const { readyState } = err.target as EventSource;
console.error("sse error", readyState); console.error("sse error", readyState, err);
// 连接还处于开启或正在连接状态 // 连接还处于开启状态
if (readyState === EventSource.OPEN || readyState === EventSource.CONNECTING) { if (readyState === EventSource.OPEN) {
return; return;
} }
initializing = false;
// 正常的关闭 // 正常的关闭
if (readyState === EventSource.CLOSED) { if (readyState === EventSource.CLOSED) {
initialized = false; initialized = false;
return; return;
} }
// 表示连接还未建立,或者连接断线
initializing = false;
// 非正常状态,目前未知 // 非正常状态,目前未知
stopStreaming(); stopStreaming();
if (inter) { if (inter) {
@@ -104,7 +105,7 @@ export default function useStreaming() {
inter = window.setTimeout(() => { inter = window.setTimeout(() => {
initialized = false; initialized = false;
startStreaming(); startStreaming();
}, 2000); }, 1000);
}; };
SSE.onmessage = (evt) => { SSE.onmessage = (evt) => {
initializing = false; initializing = false;