kb-server 0.0.1-beta.33 → 0.0.1-beta.35
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.
|
@@ -63,6 +63,13 @@ const packSSE = (sseHandlers, options) => {
|
|
|
63
63
|
if (typeof execution !== "function") {
|
|
64
64
|
throw new create_errors_1.CommonErrors.ResourceNotFound.APINotFound();
|
|
65
65
|
}
|
|
66
|
+
// 在调用 execution 前添加中断监听
|
|
67
|
+
let isClientConnected = true;
|
|
68
|
+
// 监听客户端断开事件
|
|
69
|
+
res.on("close", () => {
|
|
70
|
+
isClientConnected = false; // 标记连接已断开
|
|
71
|
+
logger_1.logger.log("info", `客户端已断开 - RequestId: ${requestId}`);
|
|
72
|
+
});
|
|
66
73
|
// 写请求头
|
|
67
74
|
res.writeHead(200, {
|
|
68
75
|
"Content-Type": "text/event-stream",
|
|
@@ -70,9 +77,21 @@ const packSSE = (sseHandlers, options) => {
|
|
|
70
77
|
Connection: "keep-alive",
|
|
71
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
|
// 完成响应
|