@@ -23,7 +23,10 @@ export class SSEServerTransport implements Transport {
23
23
/**
24
24
* Creates a new SSE server transport, which will direct the client to POST messages to the relative or absolute URL identified by `_endpoint`.
25
25
*/
26
- constructor ( private _endpoint : string ) {
26
+ constructor (
27
+ private _endpoint : string ,
28
+ private res : ServerResponse ,
29
+ ) {
27
30
this . _sessionId = randomUUID ( ) ;
28
31
}
29
32
@@ -32,24 +35,24 @@ export class SSEServerTransport implements Transport {
32
35
*
33
36
* This should be called when a GET request is made to establish the SSE stream.
34
37
*/
35
- async connectSSE ( req : IncomingMessage , res : ServerResponse ) : Promise < void > {
38
+ async start ( ) : Promise < void > {
36
39
if ( this . _sseResponse ) {
37
40
throw new Error ( "Already connected!" ) ;
38
41
}
39
42
40
- res . writeHead ( 200 , {
43
+ this . res . writeHead ( 200 , {
41
44
"Content-Type" : "text/event-stream" ,
42
45
"Cache-Control" : "no-cache" ,
43
46
Connection : "keep-alive" ,
44
47
} ) ;
45
48
46
49
// Send the endpoint event
47
- res . write (
50
+ this . res . write (
48
51
`event: endpoint\ndata: ${ encodeURI ( this . _endpoint ) } ?sessionId=${ this . _sessionId } \n\n` ,
49
52
) ;
50
53
51
- this . _sseResponse = res ;
52
- res . on ( "close" , ( ) => {
54
+ this . _sseResponse = this . res ;
55
+ this . res . on ( "close" , ( ) => {
53
56
this . _sseResponse = undefined ;
54
57
this . onclose ?.( ) ;
55
58
} ) ;
0 commit comments