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,6 +36,11 @@ export default class Engine {
} }
}) })
if (!params.disableWebSockets) {
this.io = new SocketIO.Server({
path: `/${params.refName}`,
})
this.io.attachApp(this.app.uws_instance) this.io.attachApp(this.app.uws_instance)
this.ws = global.rtengine = new rtengine({ this.ws = global.rtengine = new rtengine({
@ -48,6 +49,7 @@ export default class Engine {
io: this.io, io: this.io,
}) })
} }
}
listen = async (params) => { listen = async (params) => {
if (process.env.lb_service) { if (process.env.lb_service) {
@ -67,6 +69,7 @@ export default class Engine {
return true return true
}) })
if (!params.disableWebSockets) {
process.send({ process.send({
type: "router:ws:register", type: "router:ws:register",
id: process.env.lb_service.id, id: process.env.lb_service.id,
@ -79,7 +82,9 @@ export default class Engine {
}, },
} }
}) })
}
if (process.send) {
// try to send router map to host // try to send router map to host
process.send({ process.send({
type: "router:register", type: "router:register",
@ -95,12 +100,16 @@ export default class Engine {
} }
}) })
} }
}
await this.app.listen(this.params.listen_port) await this.app.listen(this.params.listen_port)
} }
close = async () => { close = async () => {
if (this.io) {
this.io.close() 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,11 +136,13 @@ 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 (this.engine.ws) {
if (typeof this.engine.ws?.initialize == "function") { if (typeof this.engine.ws?.initialize == "function") {
await this.engine.ws.initialize({ await this.engine.ws.initialize({
redisInstance: this.redis redisInstance: this.redis
}) })
} }
}
// if is a linebridge service then initialize IPC Channels // if is a linebridge service then initialize IPC Channels
if (process.env.lb_service) { if (process.env.lb_service) {