Commit 2dec9360 authored by 水玉婷's avatar 水玉婷
Browse files

feat:重连机制优化

parent ced88523
...@@ -117,6 +117,19 @@ export class SSEService { ...@@ -117,6 +117,19 @@ export class SSEService {
this.closeSSE(); this.closeSSE();
// 检测服务器不可用情况
const isServerUnavailable = error &&
(error.type === 'error' ||
(error.target && error.target.readyState === 0) ||
(error.message && error.message.includes('CONNECTION_REFUSED')));
if (isServerUnavailable) {
console.error('⛔ 服务器不可用,停止重连');
// 重置重连状态,避免后续重连
this.reconnectAttempts = this.MAX_RECONNECT_ATTEMPTS;
return;
}
// 简单重连逻辑 // 简单重连逻辑
if (!this.isReconnecting.value && this.reconnectAttempts < this.MAX_RECONNECT_ATTEMPTS) { if (!this.isReconnecting.value && this.reconnectAttempts < this.MAX_RECONNECT_ATTEMPTS) {
this.reconnectAttempts++; this.reconnectAttempts++;
...@@ -168,9 +181,9 @@ export class SSEService { ...@@ -168,9 +181,9 @@ export class SSEService {
this.timeArr.push(reconnectTimeout); this.timeArr.push(reconnectTimeout);
if (this.handlers.onReconnect) { // 关键修复:移除这里的onReconnect回调,真正的重连成功应该在onopen事件中触发
this.handlers.onReconnect(newDialogSessionId); // 这样只有在连接真正建立成功时才会显示重连成功消息
} console.log('⏳ 重连已启动,等待连接建立...');
} }
// 关闭SSE连接 // 关闭SSE连接
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment