From a0039ef5307e6bd9886d068d61cb345ed289a073 Mon Sep 17 00:00:00 2001 From: Archeb Date: Sun, 23 Jun 2024 09:28:17 +0800 Subject: [PATCH] callback for exit --- index.mjs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/index.mjs b/index.mjs index ec75fb6..c7f2cac 100644 --- a/index.mjs +++ b/index.mjs @@ -263,8 +263,9 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => { var url = "https://you.com/api/streamingSearch?" + req_param.toString(); console.log("正在发送请求"); session.page.evaluate( - async (url, callbackName) => { + async (url, traceId) => { var evtSource = new EventSource(url); + var callbackName = "callback" + traceId.substring(0, 8); evtSource.onerror = (error) => { window[callbackName]("error", error); evtSource.close(); @@ -292,13 +293,23 @@ app.post("/v1/messages", apiKeyAuth, (req, res) => { window[callbackName](youChatToken); } }; + // 注册退出函数 + window["exit" + traceId.substring(0, 8)] = () => { + evtSource.close(); + }; }, url, - "callback" + traceId.substring(0, 8) + traceId.substring(0, 8) ); res.on("close", function () { console.log(" > [Client closed]"); + session.page.evaluate( + (traceId) => { + window["exit" + traceId.substring(0, 8)](); + }, + traceId.substring(0, 8) + ); }); } catch (e) { console.log(e);