From bbc419bcda5591030d9461607d794986733a6fb5 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Sun, 17 Mar 2024 00:51:52 +0000 Subject: [PATCH] support disabled websocket --- package.json | 2 +- src/server/engines/hyper-express/index.js | 83 +++++++++++++---------- src/server/server.js | 11 +-- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index a8407e5..fac98cf 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "linebridge", - "version": "0.18.6", + "version": "0.17.0", "description": "API Framework for RageStudio backends", "author": "RageStudio", "main": "./dist/client/index.js", diff --git a/src/server/engines/hyper-express/index.js b/src/server/engines/hyper-express/index.js index 327356a..b08b0af 100644 --- a/src/server/engines/hyper-express/index.js +++ b/src/server/engines/hyper-express/index.js @@ -18,10 +18,6 @@ export default class Engine { ws = null init = async (params) => { - this.io = new SocketIO.Server({ - path: `/${params.refName}`, - }) - // register 404 await this.router.any("*", (req, res) => { return res.status(404).json({ @@ -40,13 +36,19 @@ export default class Engine { } }) - this.io.attachApp(this.app.uws_instance) + if (!params.disableWebSockets) { + this.io = new SocketIO.Server({ + path: `/${params.refName}`, + }) - this.ws = global.rtengine = new rtengine({ - ...params, - handleAuth: params.handleWsAuth, - io: this.io, - }) + this.io.attachApp(this.app.uws_instance) + + this.ws = global.rtengine = new rtengine({ + ...params, + handleAuth: params.handleWsAuth, + io: this.io, + }) + } } listen = async (params) => { @@ -67,40 +69,47 @@ export default class Engine { return true }) - process.send({ - type: "router:ws:register", - id: process.env.lb_service.id, - index: process.env.lb_service.index, - data: { - namespace: params.refName, - listen: { - ip: this.params.listen_ip, - port: this.params.listen_port, - }, - } - }) + if (!params.disableWebSockets) { + process.send({ + type: "router:ws:register", + id: process.env.lb_service.id, + index: process.env.lb_service.index, + data: { + namespace: params.refName, + listen: { + ip: this.params.listen_ip, + port: this.params.listen_port, + }, + } + }) + } - // try to send router map to host - process.send({ - type: "router:register", - id: process.env.lb_service.id, - index: process.env.lb_service.index, - data: { - router_map: this.router.map, - path_overrides: pathOverrides, - listen: { - ip: this.params.listen_ip, - port: this.params.listen_port, - }, - } - }) + if (process.send) { + // try to send router map to host + process.send({ + type: "router:register", + id: process.env.lb_service.id, + index: process.env.lb_service.index, + data: { + router_map: this.router.map, + path_overrides: pathOverrides, + listen: { + ip: this.params.listen_ip, + port: this.params.listen_port, + }, + } + }) + } } await this.app.listen(this.params.listen_port) } close = async () => { - this.io.close() + if (this.io) { + this.io.close() + } + await this.app.close() } } \ No newline at end of file diff --git a/src/server/server.js b/src/server/server.js index 09641ac..5821434 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -58,6 +58,7 @@ class Server { this.params.listen_port = this.constructor.listenPort ?? this.constructor.listen_port ?? this.params.listen_port ?? 3000 this.params.http_protocol = this.params.http_protocol ?? "http" this.params.http_address = `${this.params.http_protocol}://${defaults.localhost_address}:${this.params.listen_port}` + this.params.disableWebSockets = this.constructor.disableWebSockets ?? this.params.disableWebSockets ?? false this.params.routesPath = this.constructor.routesPath ?? this.params.routesPath this.params.wsRoutesPath = this.constructor.wsRoutesPath ?? this.params.wsRoutesPath @@ -135,10 +136,12 @@ class Server { await this.engine.app.use(this.engine.router) // initialize websocket init hook if needed - if (typeof this.engine.ws?.initialize == "function") { - await this.engine.ws.initialize({ - redisInstance: this.redis - }) + if (this.engine.ws) { + if (typeof this.engine.ws?.initialize == "function") { + await this.engine.ws.initialize({ + redisInstance: this.redis + }) + } } // if is a linebridge service then initialize IPC Channels