Переработаны роутеры приложения

Переписано APi WebSocket для работы с новыми роутерами
This commit is contained in:
2025-10-03 17:11:31 +03:00
parent d75fb7ec3d
commit 6ec6523d71
54 changed files with 2593 additions and 3749 deletions

6
ws/routes/connection.js Normal file
View File

@@ -0,0 +1,6 @@
const { broadcast } = require("../utils/broadcaster");
module.exports = (wss, ws, message) => {
console.log(`🔗 Connection event from ${ws.user.name}`);
broadcast(wss, { event: "user_connected", user: {name: ws.user.name, id: ws.user.id } });
};

18
ws/routes/index.js Normal file
View File

@@ -0,0 +1,18 @@
const connectionRoute = require("./connection");
const messageRoute = require("./message");
const routes = {
connection: connectionRoute,
message: messageRoute,
};
function routeMessage(wss, ws, message) {
const handler = routes[message.event];
if (handler) {
handler(wss, ws, message);
} else {
ws.send(JSON.stringify({ error: `Unknown event: ${message.event}` }));
}
}
module.exports = { routeMessage };

25
ws/routes/message.js Normal file
View File

@@ -0,0 +1,25 @@
const { updateApartment } = require("../services/apartments.service");
const { updateBuilding } = require("../services/buildings.service");
const { updateStand } = require("../services/stand.service");
const { broadcast } = require("../utils/broadcaster");
module.exports = async (wss, ws, message) => {
try {
switch (message.type) {
case "apartment":
await updateApartment(ws.user, message.data);
break;
case "building":
await updateBuilding(ws.user, message.data);
break;
case "stand":
await updateStand(ws.user, message.data);
break;
default:
return ws.send(JSON.stringify({ error: `Unknown message type: ${message.type}` }));
}
broadcast(wss, message);
} catch (err) {
ws.send(JSON.stringify({ error: err.message }));
}
};