Pārlūkot izejas kodu

Merge pull request #292 from redexp/master

Jonas Gloning 2 gadi atpakaļ
vecāks
revīzija
5e3dedcd72
3 mainītis faili ar 15 papildinājumiem un 2 dzēšanām
  1. 1 0
      index.d.ts
  2. 3 0
      src/config/index.ts
  3. 11 2
      src/services/webSocketServer/index.ts

+ 1 - 0
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 {

+ 3 - 0
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 = {

+ 11 - 2
src/services/webSocketServer/index.ts

@@ -18,7 +18,7 @@ interface IAuthParams {
   key?: string;
 }
 
-type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit'>;
+type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit' | 'createWebSocketServer'>;
 
 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));