Skip to content

Commit 2b0599e

Browse files
committed
Update server SSE transport
1 parent c39f808 commit 2b0599e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/server/sse.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export class SSEServerTransport implements Transport {
2323
/**
2424
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
2525
*/
26-
constructor(private _endpoint: string) {
26+
constructor(
27+
private _endpoint: string,
28+
private res: ServerResponse,
29+
) {
2730
this._sessionId = randomUUID();
2831
}
2932

@@ -32,24 +35,24 @@ export class SSEServerTransport implements Transport {
3235
*
3336
* This should be called when a GET request is made to establish the SSE stream.
3437
*/
35-
async connectSSE(req: IncomingMessage, res: ServerResponse): Promise<void> {
38+
async start(): Promise<void> {
3639
if (this._sseResponse) {
3740
throw new Error("Already connected!");
3841
}
3942

40-
res.writeHead(200, {
43+
this.res.writeHead(200, {
4144
"Content-Type": "text/event-stream",
4245
"Cache-Control": "no-cache",
4346
Connection: "keep-alive",
4447
});
4548

4649
// Send the endpoint event
47-
res.write(
50+
this.res.write(
4851
`event: endpoint\ndata: ${encodeURI(this._endpoint)}?sessionId=${this._sessionId}\n\n`,
4952
);
5053

51-
this._sseResponse = res;
52-
res.on("close", () => {
54+
this._sseResponse = this.res;
55+
this.res.on("close", () => {
5356
this._sseResponse = undefined;
5457
this.onclose?.();
5558
});

0 commit comments

Comments
 (0)