diff --git a/index.d.ts b/index.d.ts index c38027e..aa9f577 100644 --- a/index.d.ts +++ b/index.d.ts @@ -26,6 +26,7 @@ declare interface IConfig { cert: string; }; readonly generateClientId?: () => string; + readonly createWebSocketServer?: (options: WebSocketLib.ServerOptions) => WebSocketLib.Server; } declare interface IClient { diff --git a/src/config/index.ts b/src/config/index.ts index b048d16..ee57872 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,3 +1,5 @@ +import type {Server, ServerOptions} from 'ws'; + export interface IConfig { readonly host: string; readonly port: number; @@ -14,6 +16,7 @@ export interface IConfig { cert: string; }; readonly generateClientId?: () => string; + readonly createWebSocketServer?: (options: ServerOptions) => Server; } const defaultConfig: IConfig = { diff --git a/src/services/webSocketServer/index.ts b/src/services/webSocketServer/index.ts index a2c335e..fe5075a 100644 --- a/src/services/webSocketServer/index.ts +++ b/src/services/webSocketServer/index.ts @@ -18,7 +18,7 @@ interface IAuthParams { key?: string; } -type CustomConfig = Pick; +type CustomConfig = Pick; const WS_PATH = 'peerjs'; @@ -40,7 +40,16 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer { const path = this.config.path; this.path = `${path}${path.endsWith('/') ? "" : "/"}${WS_PATH}`; - this.socketServer = new WebSocketLib.Server({ path: this.path, server }); + const options: WebSocketLib.ServerOptions = { + path: this.path, + server, + }; + + this.socketServer = ( + config.createWebSocketServer ? + config.createWebSocketServer(options) : + new WebSocketLib.Server(options) + ); this.socketServer.on("connection", (socket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("error", (error: Error) => this._onSocketError(error));