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' });
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue