feat: add persistent connections to ws
This commit is contained in:
parent
c2f6d6b653
commit
308df5a5e6
1 changed files with 4 additions and 0 deletions
4
index.js
4
index.js
|
|
@ -26,6 +26,7 @@ let eta = new Eta({ views: viewpath, cache: false, autoEscape: true });
|
|||
|
||||
let routes = { GET: [], POST: [], PUT: [], DELETE: [] };
|
||||
const wsRoutes = {}
|
||||
const wsConnections = {}
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
|
|
@ -121,6 +122,7 @@ function bootstrapWebsocketHandler(route) {
|
|||
let handler = vm.runInContext(`${route.handler}\n\nhandler;`, context);
|
||||
|
||||
if (!wsRoutes[routeWithPrefix(route)]) {
|
||||
wsConnections[routeWithPrefix(route)] = new Set();
|
||||
// initialize ws router. future updates will only require updating
|
||||
// the wsRoutes dict, not create a whole new route
|
||||
wsRouter.ws(routeWithPrefix(route), (ws, req) => {
|
||||
|
|
@ -144,6 +146,8 @@ function bootstrapWebsocketHandler(route) {
|
|||
);
|
||||
};
|
||||
|
||||
ws.clients = wsConnections[routeWithPrefix(route)];
|
||||
|
||||
return wsRoutes[routeWithPrefix(route)](ws, req)
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue