renamed keys in json websocket send on synchronization, upgrading ws to wss

This commit is contained in:
leca 2024-11-13 11:44:37 +03:00
parent c4f0d65f4f
commit 2be40b6f34
1 changed files with 14 additions and 11 deletions

View File

@ -10,14 +10,12 @@ import ProductRouter from './routers/product.js';
import CategoryRouter from './routers/category.js'; import CategoryRouter from './routers/category.js';
import { WebSocketServer } from 'ws'; import { WebSocketServer } from 'ws';
import http from 'http'
import jwt from 'jsonwebtoken'; import jwt from 'jsonwebtoken';
const app = express(); const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ port: config.wsport }); const wss = new WebSocketServer({ port: config.wsport });
const clients = [] const clients = [];
app.use(express.urlencoded({ extended: false, limit: "200mb", parameterLimit: 100000 })); app.use(express.urlencoded({ extended: false, limit: "200mb", parameterLimit: 100000 }));
app.use(express.json({ limit: "200mb", parameterLimit: 1000000 })); app.use(express.json({ limit: "200mb", parameterLimit: 1000000 }));
@ -34,18 +32,18 @@ app.get('/status', (req, res) => {
wss.on('connection', (client) => { wss.on('connection', (client) => {
client.on('message', async (message) => { client.on('message', async (message) => {
let parsed = JSON.parse(message) let parsed = JSON.parse(message);
let token = parsed.token let token = parsed.token;
let currentGroup = parsed.currentGroup let currentGroup = parsed.currentGroup;
if (!jwt.verify(token, config.secret)) { if (!jwt.verify(token, config.secret)) {
client.send("Invalid token"); client.send("Invalid token");
return return;
} }
if (!await UserService.isInGroup(jwt.decode(token, config.secret).login.id, currentGroup)) { if (!await UserService.isInGroup(jwt.decode(token, config.secret).login.id, currentGroup)) {
client.send("Not a member of specified group") client.send("Not a member of specified group");
return return;
} }
clients.push({ clients.push({
@ -65,9 +63,14 @@ wss.on('connection', (client) => {
}) })
}); });
const server = app.listen(config.port, () => {
log.info(`Application has started on port ${config.port}`);
});
app.listen(config.port, () => { server.on('upgrade', (req, res, head) => {
log.info(`Application has started on port ${config.port}`) wss.handleUpgrade(req, res, head, socket => {
wss.emit('connection', socket, request);
});
}); });
export default clients; export default clients;