test(streaming): more testing on streaming the lofs
This commit is contained in:
@@ -14,13 +14,31 @@
|
|||||||
// // Start streaming when the button is clicked
|
// // Start streaming when the button is clicked
|
||||||
// let es;
|
// let es;
|
||||||
|
|
||||||
// const url = `http://localhost:4000/api/log`;
|
// const url = `http://localhost:4000/api/logger/logs/stream?service=ocme-count&level=info`;
|
||||||
|
|
||||||
// es = new EventSource(url, {withCredentials: true});
|
// es = new EventSource(url);
|
||||||
// es.onopen = () => console.log(">>> Connection opened!");
|
// es.onopen = () => console.log(">>> Connection opened!");
|
||||||
// es.onerror = (e) => console.log("ERROR!", e);
|
// es.onerror = (e) => console.log("ERROR!", e);
|
||||||
// es.onmessage = (e) => {
|
// es.onmessage = (e) => {
|
||||||
// console.log(">>>", JSON.stringify(e));
|
// const data = JSON.parse(e.data);
|
||||||
|
|
||||||
|
// console.log(e);
|
||||||
|
// console.log(data);
|
||||||
|
// switch (data.type) {
|
||||||
|
// case "time-update":
|
||||||
|
// console.log(data);
|
||||||
|
// break;
|
||||||
|
// case "error":
|
||||||
|
// console.log(data);
|
||||||
|
// break;
|
||||||
|
// case "done":
|
||||||
|
// console.log(data);
|
||||||
|
// es.close(); // Close the connection when done
|
||||||
|
|
||||||
|
// break;
|
||||||
|
// default:
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
// };
|
// };
|
||||||
|
|
||||||
// return () => es.close();
|
// return () => es.close();
|
||||||
|
|||||||
@@ -29,12 +29,23 @@ app.openapi(
|
|||||||
}),
|
}),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
apiHit(c, {endpoint: `api/logger/logs`});
|
apiHit(c, {endpoint: `api/logger/logs`});
|
||||||
c.header("Access-Control-Allow-Origin", "*"); // Or restrict to a specific origin
|
|
||||||
c.header("Access-Control-Allow-Headers", "Content-Type");
|
|
||||||
c.header("Content-Type", "text/event-stream");
|
c.header("Content-Type", "text/event-stream");
|
||||||
c.header("Cache-Control", "no-cache");
|
c.header("Cache-Control", "no-cache");
|
||||||
c.header("Connection", "keep-alive");
|
c.header("Connection", "keep-alive");
|
||||||
return streamLogs(c);
|
return streamSSE(c, async (stream) => {
|
||||||
|
let id = 0;
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
while (true) {
|
||||||
|
const message = `It is ${new Date().toISOString()}`;
|
||||||
|
await stream.writeSSE({
|
||||||
|
data: message,
|
||||||
|
event: "time-update",
|
||||||
|
id: String(id++),
|
||||||
|
});
|
||||||
|
encoder.encode(`data: ${JSON.stringify({type: "progress", data: id})}\n\n`);
|
||||||
|
await stream.sleep(1000);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
Reference in New Issue
Block a user