Update server SSE transport
This commit is contained in:
parent
c39f8084d7
commit
2b0599e163
1 changed files with 9 additions and 6 deletions
|
|
@ -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?.();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue