ajout de pastille pour les amis dans global chat et recuperation des messages recents

This commit is contained in:
2026-01-27 02:15:31 +01:00
parent db7062c34a
commit 3047f7593b
4 changed files with 73 additions and 5 deletions
+20 -2
View File
@@ -1,5 +1,6 @@
import jwt from 'jsonwebtoken';
import chatService from './global_chat.js';
import friendsService from './friends.js';
function setupSocketIO(io)
{
@@ -21,11 +22,27 @@ function setupSocketIO(io)
}
});
io.on('connection', (socket) =>
io.on('connection', async (socket) =>
{
console.log(`User connected: ${socket.user.username}`);
socket.join('general-chat');
// Send recent messages and friend IDs on connection
try {
const [recentMessages, friendIds] = await Promise.all([
chatService.getRecentMessages(50),
friendsService.getFriendIds(socket.user.userId)
]);
socket.emit('chat-init', {
messages: recentMessages,
friendIds: friendIds
});
} catch (err) {
console.error('Error fetching initial data:', err);
}
socket.on('chat-message', async(data) =>
{
try
@@ -33,7 +50,8 @@ function setupSocketIO(io)
const message = await chatService.saveMessage(socket.user.userId, data.content);
socket.broadcast.to('general-chat').emit('chat-message',
{
id:message.id,
id: message.id,
sender_id: socket.user.userId,
username: socket.user.username,
content: message.content,
created_at: message.created_at