@@ -38,6 +38,11 @@ export type SocketOptions = {
38
38
export type ServerOptions = SocketOptions & {
39
39
wsCreator ?: SocketFactory ;
40
40
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 ;
41
46
} ;
42
47
43
48
const defaultFactory : SocketFactory = ( options : SocketOptions ) => {
@@ -52,7 +57,7 @@ export class WebsocketServerTransport implements ServerTransport {
52
57
private readonly port : number ;
53
58
private readonly factory : SocketFactory ;
54
59
55
- constructor ( options : ServerOptions ) {
60
+ constructor ( private options : ServerOptions ) {
56
61
this . host = options . host ;
57
62
this . port = options . port ;
58
63
this . factory = options . wsCreator ?? defaultFactory ;
@@ -69,7 +74,7 @@ export class WebsocketServerTransport implements ServerTransport {
69
74
) => Multiplexer & Demultiplexer & FrameHandler
70
75
) : Promise < Closeable > {
71
76
const websocketServer : Server = await this . connectServer ( ) ;
72
- const serverCloseable = new ServerCloseable ( websocketServer ) ;
77
+ const serverCloseable = new ServerCloseable ( websocketServer , this . options . externallyCloseServer ) ;
73
78
74
79
const connectionListener = ( websocket : WebSocket ) => {
75
80
websocket . binaryType = "nodebuffer" ;
@@ -111,7 +116,7 @@ export class WebsocketServerTransport implements ServerTransport {
111
116
}
112
117
113
118
class ServerCloseable extends Deferred {
114
- constructor ( private readonly server : Server ) {
119
+ constructor ( private readonly server : Server , private externallyCloseServer ?: boolean ) {
115
120
super ( ) ;
116
121
}
117
122
@@ -121,7 +126,9 @@ class ServerCloseable extends Deferred {
121
126
return ;
122
127
}
123
128
124
- this . server . close ( ) ;
129
+ if ( ! this . externallyCloseServer ) {
130
+ this . server . close ( ) ;
131
+ }
125
132
super . close ( ) ;
126
133
}
127
134
}
0 commit comments