Skip to content

Commit

Permalink
feat: allow closing a WebSocket server outside of the ServerCloseable
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Ontong <[email protected]>
  • Loading branch information
stevensJourney committed Jun 11, 2024
1 parent b559817 commit 0d2b8b4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/rsocket-websocket-server/src/WebsocketServerTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export type SocketOptions = {
export type ServerOptions = SocketOptions & {
wsCreator?: SocketFactory;
debug?: boolean;
/**
* If enabled the ServerCloseable will not close the WebSocket
* server. The server should be closed externally.
*/
externallyCloseServer?: boolean;
};

const defaultFactory: SocketFactory = (options: SocketOptions) => {
Expand All @@ -52,7 +57,7 @@ export class WebsocketServerTransport implements ServerTransport {
private readonly port: number;
private readonly factory: SocketFactory;

constructor(options: ServerOptions) {
constructor(private options: ServerOptions) {
this.host = options.host;
this.port = options.port;
this.factory = options.wsCreator ?? defaultFactory;
Expand All @@ -69,7 +74,7 @@ export class WebsocketServerTransport implements ServerTransport {
) => Multiplexer & Demultiplexer & FrameHandler
): Promise<Closeable> {
const websocketServer: Server = await this.connectServer();
const serverCloseable = new ServerCloseable(websocketServer);
const serverCloseable = new ServerCloseable(websocketServer, this.options.externallyCloseServer);

const connectionListener = (websocket: WebSocket) => {
websocket.binaryType = "nodebuffer";
Expand Down Expand Up @@ -111,7 +116,7 @@ export class WebsocketServerTransport implements ServerTransport {
}

class ServerCloseable extends Deferred {
constructor(private readonly server: Server) {
constructor(private readonly server: Server, private externallyCloseServer?: boolean) {
super();
}

Expand All @@ -121,7 +126,9 @@ class ServerCloseable extends Deferred {
return;
}

this.server.close();
if (!this.externallyCloseServer) {
this.server.close();
}
super.close();
}
}

0 comments on commit 0d2b8b4

Please sign in to comment.