diff --git a/docs/1.guide/6.websocket.md b/docs/1.guide/6.websocket.md index 5f9603cf..cda30f30 100644 --- a/docs/1.guide/6.websocket.md +++ b/docs/1.guide/6.websocket.md @@ -100,10 +100,9 @@ router.get( await eventStream.push("Hello world"); }, 1000); - // cleanup the interval and close the stream when the connection is terminated - eventStream.onClosed(async () => { + // cleanup the interval when the connection is terminated or the writer is closed + eventStream.onClosed(() => { clearInterval(interval); - await eventStream.close(); }); return eventStream.send(); diff --git a/examples/server-sent-events.ts b/examples/server-sent-events.ts index 5902f4ca..979750cf 100644 --- a/examples/server-sent-events.ts +++ b/examples/server-sent-events.ts @@ -15,10 +15,9 @@ router.get( await eventStream.push("Hello world"); }, 1000); - // cleanup the interval and close the stream when the connection is terminated - eventStream.onClosed(async () => { + // cleanup the interval when the connection is terminated or the writer is closed + eventStream.onClosed(() => { clearInterval(interval); - await eventStream.close(); }); return eventStream.send(); diff --git a/src/utils/sse/event-stream.ts b/src/utils/sse/event-stream.ts index 6e6eef64..d278958e 100644 --- a/src/utils/sse/event-stream.ts +++ b/src/utils/sse/event-stream.ts @@ -150,11 +150,9 @@ export class EventStream { /** * Triggers callback when the writable stream is closed. * It is also triggered after calling the `close()` method. - * It also triggers when the request connection has been closed by either the client or the server. */ onClosed(cb: () => any) { this._writer.closed.then(cb); - this._h3Event.node?.req.on("close", cb); } async send() { diff --git a/test/sse.test.ts b/test/sse.test.ts index e18db2e6..2c1b5bd5 100644 --- a/test/sse.test.ts +++ b/test/sse.test.ts @@ -34,8 +34,7 @@ describe("Server Sent Events (SSE)", () => { } eventStream.push("hello world"); }); - eventStream.onClosed(async () => { - await eventStream.close(); + eventStream.onClosed(() => { clearInterval(interval); }); return eventStream.send();