added peerWs to app, added ws config

This commit is contained in:
Sergii KLiuchnyk 2022-09-30 12:25:23 +03:00
parent b12a4a3a15
commit a77de706c7
5 changed files with 26 additions and 14 deletions

10
dist/src/instance.js vendored
View File

@ -27,12 +27,12 @@ const createInstance = ({ app, server, options }) => {
app.use(options.path, api); app.use(options.path, api);
//use mountpath for WS server //use mountpath for WS server
const customConfig = Object.assign(Object.assign({}, config), { path: path_1.default.posix.join(app.path(), options.path, '/') }); const customConfig = Object.assign(Object.assign({}, config), { path: path_1.default.posix.join(app.path(), options.path, '/') });
const wss = new webSocketServer_1.WebSocketServer({ const wss2 = new webSocketServer_1.WebSocketServer({
server, server,
realm, realm,
config: customConfig config: customConfig
}); });
wss.on("connection", (client) => { wss2.on("connection", (client) => {
const messageQueue = realm.getMessageQueueById(client.getId()); const messageQueue = realm.getMessageQueueById(client.getId());
if (messageQueue) { if (messageQueue) {
let message; let message;
@ -43,14 +43,14 @@ const createInstance = ({ app, server, options }) => {
} }
app.emit("connection", client); app.emit("connection", client);
}); });
wss.on("message", (client, message) => { wss2.on("message", (client, message) => {
app.emit("message", client, message); app.emit("message", client, message);
messageHandler.handle(client, message); messageHandler.handle(client, message);
}); });
wss.on("close", (client) => { wss2.on("close", (client) => {
app.emit("disconnect", client); app.emit("disconnect", client);
}); });
wss.on("error", (error) => { wss2.on("error", (error) => {
app.emit("error", error); app.emit("error", error);
}); });
messagesExpire.startMessagesExpiration(); messagesExpire.startMessagesExpiration();

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "peer", "name": "peer",
"version": "0.6.1", "version": "0.6.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,3 +1,5 @@
import {ServerOptions as WsConfig} 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 ws?: WsConfig;
} }
const defaultConfig: IConfig = { const defaultConfig: IConfig = {

View File

@ -34,15 +34,20 @@ export const createInstance = ({ app, server, options }: {
app.use(options.path, api); app.use(options.path, api);
//use mountpath for WS server //use mountpath for WS server
const customConfig = { ...config, path: path.posix.join(app.path(), options.path, '/') }; const customConfig = {
...config,
path: path.posix.join(app.path(), options.path, '/'),
};
const wss2: IWebSocketServer = new WebSocketServer({ const wss: IWebSocketServer = new WebSocketServer({
server, server,
realm, realm,
config: customConfig config: customConfig
}); });
wss2.on("connection", (client: IClient) => { app.set('peerWs', wss);
wss.on("connection", (client: IClient) => {
const messageQueue = realm.getMessageQueueById(client.getId()); const messageQueue = realm.getMessageQueueById(client.getId());
if (messageQueue) { if (messageQueue) {
@ -57,16 +62,16 @@ export const createInstance = ({ app, server, options }: {
app.emit("connection", client); app.emit("connection", client);
}); });
wss2.on("message", (client: IClient, message: IMessage) => { wss.on("message", (client: IClient, message: IMessage) => {
app.emit("message", client, message); app.emit("message", client, message);
messageHandler.handle(client, message); messageHandler.handle(client, message);
}); });
wss2.on("close", (client: IClient) => { wss.on("close", (client: IClient) => {
app.emit("disconnect", client); app.emit("disconnect", client);
}); });
wss2.on("error", (error: Error) => { wss.on("error", (error: Error) => {
app.emit("error", error); app.emit("error", error);
}); });

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' | 'ws'>;
const WS_PATH = 'peerjs'; const WS_PATH = 'peerjs';
@ -40,7 +40,11 @@ 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 }); this.socketServer = new WebSocketLib.Server({
path: this.path,
server,
...this.config.ws,
});
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));