This commit is contained in:
Dobromir Popov
2024-06-10 14:45:52 +03:00
parent 407affa979
commit 43f3f9a281
2 changed files with 100 additions and 31 deletions

View File

@ -23,7 +23,8 @@ let storeRecordings = false;
let queueCounter = 0;
const sessions = new Map();
const users = new Map(); // Store users with their usernames and session IDs
const users = new Map();
const chats = new Map(); // Store chat rooms
storage.init().then(() => {
storage.getItem('language').then((value) => {
@ -52,6 +53,11 @@ wss.on('connection', (ws) => {
const { username } = data;
users.set(ws.sessionId, { username, sessionId: ws.sessionId });
broadcastUserList();
} else if (data.type === 'startChat') {
const { users: chatUsers } = data;
const chatId = Math.random().toString(36).substring(2);
chats.set(chatId, { participants: [ws.sessionId, ...chatUsers], messages: [] });
broadcastUserList();
} else if (data.type === 'audio') {
handleAudioData(ws, data.audio);
}
@ -187,6 +193,12 @@ app.post('/upload', (req, res) => {
});
});
app.get('/chats', (req, res) => {
const { username } = req.query;
const userChats = Array.from(chats.values()).filter(chat => chat.participants.includes(username));
res.status(200).send({ chats: userChats });
});
app.listen(PORT_HTTP, () => {
console.log(`Server listening on port ${PORT_HTTP}`);
});