Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow closing a WebSocket server outside of the ServerCloseable #279

Open
wants to merge 1 commit into
base: 1.0.x-alpha
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}