ajout de pastille pour les amis dans global chat et recuperation des messages recents
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user