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