Update server SSE transport

This commit is contained in:
Justin Spahr-Summers 2024-10-11 16:46:30 +01:00
commit 2b0599e163
No known key found for this signature in database
GPG key ID: E78498BF3C053CA7

View file

@ -23,7 +23,10 @@ export class SSEServerTransport implements Transport {
/**
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
*/
constructor(private _endpoint: string) {
constructor(
private _endpoint: string,
private res: ServerResponse,
) {
this._sessionId = randomUUID();
}
@ -32,24 +35,24 @@ export class SSEServerTransport implements Transport {
*
* This should be called when a GET request is made to establish the SSE stream.
*/
async connectSSE(req: IncomingMessage, res: ServerResponse): Promise<void> {
async start(): Promise<void> {
if (this._sseResponse) {
throw new Error("Already connected!");
}
res.writeHead(200, {
this.res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
});
// Send the endpoint event
res.write(
this.res.write(
`event: endpoint\ndata: ${encodeURI(this._endpoint)}?sessionId=${this._sessionId}\n\n`,
);
this._sseResponse = res;
res.on("close", () => {
this._sseResponse = this.res;
this.res.on("close", () => {
this._sseResponse = undefined;
this.onclose?.();
});