kb-server 0.0.1-beta.32 → 0.0.1-beta.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -69,10 +69,29 @@ const packSSE = (sseHandlers, options) => {
|
|
|
69
69
|
"Cache-Control": "no-cache",
|
|
70
70
|
Connection: "keep-alive",
|
|
71
71
|
});
|
|
72
|
+
// 在调用 execution 前添加中断监听
|
|
73
|
+
let isClientConnected = true;
|
|
74
|
+
// 监听客户端断开事件
|
|
75
|
+
req.on("close", () => {
|
|
76
|
+
isClientConnected = false; // 标记连接已断开
|
|
77
|
+
logger_1.logger.log("info", `客户端已断开 - RequestId: ${requestId}`);
|
|
78
|
+
});
|
|
72
79
|
const send = (event, message) => {
|
|
80
|
+
if (!isClientConnected) {
|
|
81
|
+
logger_1.logger.log("warning", "客户端已断开,停止发送");
|
|
82
|
+
return; // 终止发送逻辑
|
|
83
|
+
}
|
|
73
84
|
const response = (0, exports.createSSEMessage)(event, message);
|
|
74
|
-
|
|
75
|
-
|
|
85
|
+
try {
|
|
86
|
+
res.write(response);
|
|
87
|
+
logger_1.logger.log("info", `发送消息:\n${response}`);
|
|
88
|
+
}
|
|
89
|
+
catch (err) {
|
|
90
|
+
if (err.code === "EPIPE" || err.code === "ECONNRESET") {
|
|
91
|
+
logger_1.logger.log("error", "客户端连接已关闭");
|
|
92
|
+
isClientConnected = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
76
95
|
};
|
|
77
96
|
await execution(params, ctx, { send });
|
|
78
97
|
// 完成响应
|
|
@@ -113,8 +132,8 @@ const packSSE = (sseHandlers, options) => {
|
|
|
113
132
|
logger_1.logger.log("info", `发送消息:\n${response}`);
|
|
114
133
|
logger_1.logger.log("info", `耗时:${took} ms - RequestId: ${requestId}`);
|
|
115
134
|
res.write(response);
|
|
116
|
-
// 延迟
|
|
117
|
-
await (0, sleep_1.sleep)(
|
|
135
|
+
// 延迟100ms关闭连接
|
|
136
|
+
await (0, sleep_1.sleep)(100);
|
|
118
137
|
return res.end();
|
|
119
138
|
}
|
|
120
139
|
};
|