generated from lucien/api-template
fix: improved websocket usage
This commit is contained in:
parent
fbf7821320
commit
132b1f3c06
7 changed files with 135 additions and 79 deletions
|
@ -11,16 +11,6 @@ router.get('/', async (req, res) => {
|
|||
res.send(channels);
|
||||
});
|
||||
|
||||
const sockets = new Set();
|
||||
|
||||
router.ws('/', function(ws, req) {
|
||||
sockets.add(ws);
|
||||
|
||||
ws.on('close', () => {
|
||||
sockets.delete(ws);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:name', async (req, res) => {
|
||||
const name = req.params.name;
|
||||
const connection = await getConnection();
|
||||
|
@ -33,22 +23,6 @@ router.get('/:name', async (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
const channelSockets = new Map();
|
||||
|
||||
router.ws('/:name', function(ws, req) {
|
||||
const name = req.params.name;
|
||||
|
||||
if (!channelSockets.has(name)) {
|
||||
channelSockets.set(name, new Set());
|
||||
}
|
||||
|
||||
channelSockets.get(name).add(ws);
|
||||
|
||||
ws.on('close', () => {
|
||||
channelSockets.get(name).delete(ws);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:name/messages', async (req, res) => {
|
||||
const name = req.params.name;
|
||||
const connection = await getConnection();
|
||||
|
@ -105,21 +79,11 @@ router.post('/:name/messages/send', async (req, res) => {
|
|||
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channelSockets.has(name)) {
|
||||
for (const client of channelSockets.get(name)) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
req.sockets.emit({
|
||||
type: 'new_message',
|
||||
channel_id: channel[0].id,
|
||||
user_id: user.id,
|
||||
});
|
||||
|
||||
res.send({ message: 'Message sent' });
|
||||
});
|
||||
|
@ -157,21 +121,11 @@ router.post('/:name/messages/delete', async (req, res) => {
|
|||
await deleMentions(connection, message_id);
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('delete_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (channelSockets.has(name)) {
|
||||
for (const client of channelSockets.get(name)) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('delete_message');
|
||||
}
|
||||
}
|
||||
}
|
||||
req.sockets.emit({
|
||||
type: 'delete_message',
|
||||
channel_id: channel[0].id,
|
||||
user_id: user.id,
|
||||
});
|
||||
|
||||
res.send({ message: 'Message deleted' });
|
||||
});
|
||||
|
@ -201,13 +155,9 @@ router.post('/add', async (req, res) => {
|
|||
await addChannel(connection, name, description, user.id);
|
||||
connection.end();
|
||||
|
||||
if (sockets.size > 0) {
|
||||
for (const client of sockets) {
|
||||
if (client.readyState === 1) {
|
||||
client.send('new_channel');
|
||||
}
|
||||
}
|
||||
}
|
||||
req.sockets.emit({
|
||||
type: 'new_channel'
|
||||
});
|
||||
|
||||
res.send({ message: 'Channel added' });
|
||||
});
|
||||
|
|
|
@ -14,6 +14,10 @@ app.use(express.json());
|
|||
app.use(cookieParser());
|
||||
app.use(cors());
|
||||
|
||||
const { router, socketsMiddleware } = require("./libs/middlewares");
|
||||
app.use("/api/ws", router);
|
||||
app.use(socketsMiddleware);
|
||||
|
||||
function loadRoutes(folderName) {
|
||||
const routesPath = path.join(__dirname, folderName);
|
||||
const files = fs.readdirSync(routesPath);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getUser } = require('./mysql');
|
||||
const express = require('express');
|
||||
|
||||
async function checkAuth(req, res, next) {
|
||||
const { token } = req.body;
|
||||
|
@ -23,6 +24,30 @@ async function checkAuth(req, res, next) {
|
|||
}
|
||||
}
|
||||
|
||||
const router = express.Router();
|
||||
const sockets = new Set();
|
||||
|
||||
router.ws('/', function(ws, req) {
|
||||
sockets.add(ws);
|
||||
|
||||
ws.on('close', () => {
|
||||
sockets.delete(ws);
|
||||
});
|
||||
});
|
||||
|
||||
function socketsMiddleware(req, res, next) {
|
||||
req.sockets = {
|
||||
emit: function(data) {
|
||||
for (const socket of sockets) {
|
||||
socket.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
};
|
||||
next();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkAuth,
|
||||
router,
|
||||
socketsMiddleware,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue