createWebSocketServer option

This commit is contained in:
Sergii KLiuchnyk 2022-11-01 13:06:11 +02:00
parent e2854ca9b1
commit 077260a3a4
5 changed files with 18 additions and 12 deletions

View File

@ -32,8 +32,6 @@ const createInstance = ({ app, server, options }) => {
realm, realm,
config: customConfig config: customConfig
}); });
app.set('peerWs', wss);
app.emit('peerWs', wss);
wss.on("connection", (client) => { wss.on("connection", (client) => {
const messageQueue = realm.getMessageQueueById(client.getId()); const messageQueue = realm.getMessageQueueById(client.getId());
if (messageQueue) { if (messageQueue) {

View File

@ -18,7 +18,13 @@ class WebSocketServer extends events_1.default {
this.config = config; this.config = config;
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 ws_1.default.Server(Object.assign({ path: this.path, server }, this.config.ws)); const options = {
path: this.path,
server,
};
this.socketServer = (config.createWebSocketServer ?
config.createWebSocketServer(options) :
new ws_1.default.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) => this._onSocketError(error)); this.socketServer.on("error", (error) => this._onSocketError(error));
} }

View File

@ -1,4 +1,4 @@
import {ServerOptions as WsConfig} from 'ws'; import {Server, ServerOptions} from 'ws';
export interface IConfig { export interface IConfig {
readonly host: string; readonly host: string;
@ -16,7 +16,7 @@ export interface IConfig {
cert: string; cert: string;
}; };
readonly generateClientId?: () => string; readonly generateClientId?: () => string;
readonly ws?: WsConfig; readonly createWebSocketServer?: (options: ServerOptions) => Server;
} }
const defaultConfig: IConfig = { const defaultConfig: IConfig = {

View File

@ -45,9 +45,6 @@ export const createInstance = ({ app, server, options }: {
config: customConfig config: customConfig
}); });
app.set('peerWs', wss);
app.emit('peerWs', wss);
wss.on("connection", (client: IClient) => { wss.on("connection", (client: IClient) => {
const messageQueue = realm.getMessageQueueById(client.getId()); const messageQueue = realm.getMessageQueueById(client.getId());

View File

@ -18,7 +18,7 @@ interface IAuthParams {
key?: string; key?: string;
} }
type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit' | 'ws'>; type CustomConfig = Pick<IConfig, 'path' | 'key' | 'concurrent_limit' | 'createWebSocketServer'>;
const WS_PATH = 'peerjs'; const WS_PATH = 'peerjs';
@ -40,11 +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({ const options = {
path: this.path, path: this.path,
server, server,
...this.config.ws, };
});
this.socketServer = (
config.createWebSocketServer ?
config.createWebSocketServer(options) :
new WebSocketLib.Server(options)
);
this.socketServer.on("connection", (socket: MyWebSocket, req) => this._onSocketConnection(socket, req)); this.socketServer.on("connection", (socket: MyWebSocket, req) => this._onSocketConnection(socket, req));
this.socketServer.on("error", (error: Error) => this._onSocketError(error)); this.socketServer.on("error", (error: Error) => this._onSocketError(error));