feat: add persistent connections to ws

This commit is contained in:
Your Name 2025-01-21 18:13:31 -05:00
parent c2f6d6b653
commit 308df5a5e6

View file

@ -26,6 +26,7 @@ let eta = new Eta({ views: viewpath, cache: false, autoEscape: true });
let routes = { GET: [], POST: [], PUT: [], DELETE: [] }; let routes = { GET: [], POST: [], PUT: [], DELETE: [] };
const wsRoutes = {} const wsRoutes = {}
const wsConnections = {}
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); app.use(bodyParser.json());
@ -121,6 +122,7 @@ function bootstrapWebsocketHandler(route) {
let handler = vm.runInContext(`${route.handler}\n\nhandler;`, context); let handler = vm.runInContext(`${route.handler}\n\nhandler;`, context);
if (!wsRoutes[routeWithPrefix(route)]) { if (!wsRoutes[routeWithPrefix(route)]) {
wsConnections[routeWithPrefix(route)] = new Set();
// initialize ws router. future updates will only require updating // initialize ws router. future updates will only require updating
// the wsRoutes dict, not create a whole new route // the wsRoutes dict, not create a whole new route
wsRouter.ws(routeWithPrefix(route), (ws, req) => { wsRouter.ws(routeWithPrefix(route), (ws, req) => {
@ -144,6 +146,8 @@ function bootstrapWebsocketHandler(route) {
); );
}; };
ws.clients = wsConnections[routeWithPrefix(route)];
return wsRoutes[routeWithPrefix(route)](ws, req) return wsRoutes[routeWithPrefix(route)](ws, req)
}); });
} }