renamed keys in json websocket send on synchronization, upgrading ws to wss
This commit is contained in:
parent
c4f0d65f4f
commit
2be40b6f34
25
src/index.js
25
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;
|
Loading…
Reference in New Issue