Merge pull request #292 from redexp/master

This commit is contained in:
Jonas Gloning 2023-01-06 10:24:23 +01:00 committed by GitHub
commit 5e3dedcd72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

1
index.d.ts vendored
View File

@ -26,6 +26,7 @@ declare interface IConfig {
cert: string; cert: string;
}; };
readonly generateClientId?: () => string; readonly generateClientId?: () => string;
readonly createWebSocketServer?: (options: WebSocketLib.ServerOptions) => WebSocketLib.Server;
} }
declare interface IClient { declare interface IClient {

View File

@ -1,3 +1,5 @@
import type {Server, ServerOptions} from 'ws';
export interface IConfig { export interface IConfig {
readonly host: string; readonly host: string;
readonly port: number; readonly port: number;
@ -14,6 +16,7 @@ export interface IConfig {
cert: string; cert: string;
}; };
readonly generateClientId?: () => string; readonly generateClientId?: () => string;
readonly createWebSocketServer?: (options: ServerOptions) => Server;
} }
const defaultConfig: IConfig = { const defaultConfig: IConfig = {

View File

@ -18,7 +18,7 @@ interface IAuthParams {
key?: string; key?: string;
} }
type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit'>; type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit' | 'createWebSocketServer'>;
const WS_PATH = 'peerjs'; const WS_PATH = 'peerjs';
@ -40,7 +40,16 @@ export class WebSocketServer extends EventEmitter implements IWebSocketServer {
const path = this.config.path; const path = this.config.path;
this.path = `${path}${path.endsWith('/') ? "" : "/"}${WS_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("connection", (socket, req) => this._onSocketConnection(socket, req));
this.socketServer.on("error", (error: Error) => this._onSocketError(error)); this.socketServer.on("error", (error: Error) => this._onSocketError(error));