support disabled websocket

This commit is contained in:
SrGooglo 2024-03-17 00:51:52 +00:00
parent 153ebcf4cc
commit bbc419bcda
3 changed files with 54 additions and 42 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "linebridge", "name": "linebridge",
"version": "0.18.6", "version": "0.17.0",
"description": "API Framework for RageStudio backends", "description": "API Framework for RageStudio backends",
"author": "RageStudio", "author": "RageStudio",
"main": "./dist/client/index.js", "main": "./dist/client/index.js",

View File

@ -18,10 +18,6 @@ export default class Engine {
ws = null ws = null
init = async (params) => { init = async (params) => {
this.io = new SocketIO.Server({
path: `/${params.refName}`,
})
// register 404 // register 404
await this.router.any("*", (req, res) => { await this.router.any("*", (req, res) => {
return res.status(404).json({ 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({ this.io.attachApp(this.app.uws_instance)
...params,
handleAuth: params.handleWsAuth, this.ws = global.rtengine = new rtengine({
io: this.io, ...params,
}) handleAuth: params.handleWsAuth,
io: this.io,
})
}
} }
listen = async (params) => { listen = async (params) => {
@ -67,40 +69,47 @@ export default class Engine {
return true return true
}) })
process.send({ if (!params.disableWebSockets) {
type: "router:ws:register", process.send({
id: process.env.lb_service.id, type: "router:ws:register",
index: process.env.lb_service.index, id: process.env.lb_service.id,
data: { index: process.env.lb_service.index,
namespace: params.refName, data: {
listen: { namespace: params.refName,
ip: this.params.listen_ip, listen: {
port: this.params.listen_port, ip: this.params.listen_ip,
}, port: this.params.listen_port,
} },
}) }
})
}
// try to send router map to host if (process.send) {
process.send({ // try to send router map to host
type: "router:register", process.send({
id: process.env.lb_service.id, type: "router:register",
index: process.env.lb_service.index, id: process.env.lb_service.id,
data: { index: process.env.lb_service.index,
router_map: this.router.map, data: {
path_overrides: pathOverrides, router_map: this.router.map,
listen: { path_overrides: pathOverrides,
ip: this.params.listen_ip, listen: {
port: this.params.listen_port, ip: this.params.listen_ip,
}, port: this.params.listen_port,
} },
}) }
})
}
} }
await this.app.listen(this.params.listen_port) await this.app.listen(this.params.listen_port)
} }
close = async () => { close = async () => {
this.io.close() if (this.io) {
this.io.close()
}
await this.app.close() await this.app.close()
} }
} }

View File

@ -58,6 +58,7 @@ class Server {
this.params.listen_port = this.constructor.listenPort ?? this.constructor.listen_port ?? this.params.listen_port ?? 3000 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_protocol = this.params.http_protocol ?? "http"
this.params.http_address = `${this.params.http_protocol}://${defaults.localhost_address}:${this.params.listen_port}` 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.routesPath = this.constructor.routesPath ?? this.params.routesPath
this.params.wsRoutesPath = this.constructor.wsRoutesPath ?? this.params.wsRoutesPath this.params.wsRoutesPath = this.constructor.wsRoutesPath ?? this.params.wsRoutesPath
@ -135,10 +136,12 @@ class Server {
await this.engine.app.use(this.engine.router) await this.engine.app.use(this.engine.router)
// initialize websocket init hook if needed // initialize websocket init hook if needed
if (typeof this.engine.ws?.initialize == "function") { if (this.engine.ws) {
await this.engine.ws.initialize({ if (typeof this.engine.ws?.initialize == "function") {
redisInstance: this.redis await this.engine.ws.initialize({
}) redisInstance: this.redis
})
}
} }
// if is a linebridge service then initialize IPC Channels // if is a linebridge service then initialize IPC Channels