Переработаны роутеры приложения
Переписано APi WebSocket для работы с новыми роутерами
This commit is contained in:
13
ws_old/Dockerfile
Normal file
13
ws_old/Dockerfile
Normal file
@@ -0,0 +1,13 @@
|
||||
FROM node:20.18
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json package-lock.json ./
|
||||
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 4001
|
||||
|
||||
CMD npm start
|
||||
1644
ws_old/package-lock.json
generated
Normal file
1644
ws_old/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
ws_old/package.json
Normal file
18
ws_old/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "WS Sheep Service",
|
||||
"version": "1.0.1",
|
||||
"main": "ws.js",
|
||||
"scripts": {
|
||||
"start": "node ws.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"sqlite3": "^5.1.7",
|
||||
"url": "^0.11.4",
|
||||
"ws": "^8.18.0",
|
||||
"dotenv": "^17.2.0"
|
||||
}
|
||||
}
|
||||
260
ws_old/ws.js
Normal file
260
ws_old/ws.js
Normal file
@@ -0,0 +1,260 @@
|
||||
const WebSocket = require("ws");
|
||||
const { URL } = require('url');
|
||||
const sqlite3 = require('sqlite3');
|
||||
const path = require('path');
|
||||
require("dotenv").config();
|
||||
|
||||
const dbPath = process.env.DATABASE_PATH || '../';
|
||||
const db = new sqlite3.Database(path.join(dbPath, 'database.sqlite'));
|
||||
|
||||
const port = 4004;
|
||||
const api_version = '1.0.0';
|
||||
|
||||
const wss = new WebSocket.Server({
|
||||
port: port
|
||||
}, () => console.log(`Server started on port ${port}`));
|
||||
|
||||
|
||||
wss.on('connection', async (ws, request) => {
|
||||
const url = new URL(request.url, `http://${request.headers.host}`)
|
||||
const params = Object.fromEntries(url.searchParams.entries());
|
||||
const uuid = params.uuid;
|
||||
|
||||
if (!uuid) return
|
||||
|
||||
let check = await checkUUID(uuid);
|
||||
|
||||
console.log(check);
|
||||
|
||||
|
||||
if (!check && check.possibilities.can_view_territory) return
|
||||
|
||||
// Periodic ping to maintain a connection
|
||||
const pingInterval = setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.ping('ping');
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
|
||||
ws.send(JSON.stringify({ "connection": "success", "api_version": api_version }));
|
||||
|
||||
ws.on('message', (message) => {
|
||||
message = JSON.parse(message);
|
||||
|
||||
console.log(message.username, check.name);
|
||||
|
||||
switch (message.event) {
|
||||
case "connection":
|
||||
broadcastMessage(message);
|
||||
break;
|
||||
|
||||
case "message":
|
||||
updateDatabase(message);
|
||||
broadcastMessage(message);
|
||||
break;
|
||||
};
|
||||
});
|
||||
|
||||
ws.on('pong', (data) => {
|
||||
console.log('PONG received from the client:', data.toString());
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Client close');
|
||||
});
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.error('ERROR WebSocket:', err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function broadcastMessage(message) {
|
||||
wss.clients.forEach(client => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(JSON.stringify(message));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateDatabase(message) {
|
||||
|
||||
console.log(message.type);
|
||||
|
||||
|
||||
if (message.type === "apartment") {
|
||||
|
||||
let sql = 'UPDATE apartments SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
|
||||
let sql = `INSERT INTO apartments_history (apartments_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
Math.floor(Date.now())
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Apartments not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if(message.type === "building") {
|
||||
let sql = 'UPDATE buildings SET status = ?, description = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
|
||||
let sql = `INSERT INTO buildings_history (buildings_id, status, description, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.description,
|
||||
message.data.sheep_id,
|
||||
Math.floor(Date.now())
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Apartments not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if(message.type === "stand") {
|
||||
let sql = 'UPDATE stand SET status = ?, sheep_id = ?, updated_at = ? WHERE id = ?';
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.status),
|
||||
message.data.sheep_id,
|
||||
message.data.updated_at,
|
||||
message.data.id
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Product not found');
|
||||
} else {
|
||||
console.log({ "update": "ok", "id": message.data.id });
|
||||
|
||||
let sql = `INSERT INTO stand_history (stand_id, status, sheep_id, created_at) VALUES (?, ?, ?, ?, ?)`;
|
||||
|
||||
db.run(sql, [
|
||||
Number(message.data.id),
|
||||
Number(message.data.status),
|
||||
message.data.sheep_id,
|
||||
Math.floor(Date.now())
|
||||
], function (err) {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
} else if (this.changes === 0) {
|
||||
console.error('Stand not found');
|
||||
} else {
|
||||
console.log({ "insert": "ok", "id": this.lastID });
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUUID(uuid) {
|
||||
return new Promise((res, rej) => {
|
||||
db.get(`
|
||||
SELECT
|
||||
sheeps.*,
|
||||
possibilities.can_view_territory AS can_view_territory
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
possibilities ON possibilities.sheep_id = sheeps.id
|
||||
WHERE
|
||||
sheeps.uuid_manager = ?`,
|
||||
[uuid],
|
||||
(err, moderator) => {
|
||||
if (moderator) {
|
||||
let data = {
|
||||
id: moderator.sheep_id,
|
||||
group_id: moderator.group_id,
|
||||
name: moderator.name,
|
||||
icon: moderator.icon,
|
||||
uuid: moderator.uuid,
|
||||
appointment: moderator.appointment,
|
||||
sheepRole: moderator.mode_title,
|
||||
possibilities: {
|
||||
can_view_territory: moderator.can_view_territory == 1 ? true : false
|
||||
}
|
||||
}
|
||||
return res(data);
|
||||
}
|
||||
|
||||
db.get(`
|
||||
SELECT
|
||||
sheeps.*,
|
||||
possibilities.can_view_territory AS can_view_territory
|
||||
FROM
|
||||
sheeps
|
||||
LEFT JOIN
|
||||
possibilities ON possibilities.sheep_id = sheeps.id
|
||||
WHERE
|
||||
sheeps.uuid = ?`,
|
||||
[uuid],
|
||||
(err, sheep) => {
|
||||
if (sheep) {
|
||||
let data = {
|
||||
id: sheep.sheep_id,
|
||||
group_id: sheep.group_id,
|
||||
name: sheep.name,
|
||||
icon: sheep.icon,
|
||||
uuid: sheep.uuid,
|
||||
appointment: sheep.appointment,
|
||||
sheepRole: sheep.mode_title,
|
||||
possibilities: {
|
||||
can_view_territory: sheep.can_view_territory == 1 ? true : false
|
||||
}
|
||||
}
|
||||
return res(data);
|
||||
}
|
||||
|
||||
return res(false);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user