@@ -38,6 +38,11 @@ export type SocketOptions = {
3838export type ServerOptions = SocketOptions & {
3939 wsCreator ?: SocketFactory ;
4040 debug ?: boolean ;
41+ /**
42+ * If enabled the ServerCloseable will not close the WebSocket
43+ * server. The server should be closed externally.
44+ */
45+ externallyCloseServer ?: boolean ;
4146} ;
4247
4348const defaultFactory : SocketFactory = ( options : SocketOptions ) => {
@@ -52,7 +57,7 @@ export class WebsocketServerTransport implements ServerTransport {
5257 private readonly port : number ;
5358 private readonly factory : SocketFactory ;
5459
55- constructor ( options : ServerOptions ) {
60+ constructor ( private options : ServerOptions ) {
5661 this . host = options . host ;
5762 this . port = options . port ;
5863 this . factory = options . wsCreator ?? defaultFactory ;
@@ -69,7 +74,7 @@ export class WebsocketServerTransport implements ServerTransport {
6974 ) => Multiplexer & Demultiplexer & FrameHandler
7075 ) : Promise < Closeable > {
7176 const websocketServer : Server = await this . connectServer ( ) ;
72- const serverCloseable = new ServerCloseable ( websocketServer ) ;
77+ const serverCloseable = new ServerCloseable ( websocketServer , this . options . externallyCloseServer ) ;
7378
7479 const connectionListener = ( websocket : WebSocket ) => {
7580 websocket . binaryType = "nodebuffer" ;
@@ -111,7 +116,7 @@ export class WebsocketServerTransport implements ServerTransport {
111116}
112117
113118class ServerCloseable extends Deferred {
114- constructor ( private readonly server : Server ) {
119+ constructor ( private readonly server : Server , private externallyCloseServer ?: boolean ) {
115120 super ( ) ;
116121 }
117122
@@ -121,7 +126,9 @@ class ServerCloseable extends Deferred {
121126 return ;
122127 }
123128
124- this . server . close ( ) ;
129+ if ( ! this . externallyCloseServer ) {
130+ this . server . close ( ) ;
131+ }
125132 super . close ( ) ;
126133 }
127134}
0 commit comments