fixed messages displaying multiple times

This commit is contained in:
slawk0
2024-08-25 15:34:27 +02:00
parent 300bedddbc
commit 34440563db
3 changed files with 4 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
Web chat (not finished!) Web chat (not finished!)
======================== ========================
Project for hackclub arcade.
Web chat application using Express.js, Socket.io, bcrypt, Postgresql, and JWT for user authentication . Web chat application using Express.js, Socket.io, bcrypt, Postgresql, and JWT for user authentication .
Selfhost: Selfhost:

View File

@@ -81,7 +81,8 @@ function initializeSocket(server) {
for (const row of result.rows) { for (const row of result.rows) {
if (row.username === username || row.recipient === username) { if (row.username === username || row.recipient === username) {
socket.emit('chat message', { username: row.username, recipient: row.recipient, content: row.content} ); io.to(username).to(recipient).emit('chat message', { username: newMessage.username, recipient: newMessage.recipient, content: newMessage.content });
} }
} }
} catch (e) { } catch (e) {
@@ -100,10 +101,6 @@ function initializeSocket(server) {
`; `;
const result = await db.query(query, [username, recipient]); const result = await db.query(query, [username, recipient]);
// const formattedMessages = result.rows.map(row =>
// `${row.username} to ${row.recipient}: ${row.content}`
// );
if (result.rows.length > 0) { if (result.rows.length > 0) {
//const { username: sender, recipient: receiver, content: content, id: id } = result.rows; //const { username: sender, recipient: receiver, content: content, id: id } = result.rows;
console.log('Sending messages:', result.rows); console.log('Sending messages:', result.rows);

View File

@@ -50,7 +50,7 @@ async function initializeSocket() {
console.log('Connected to server'); console.log('Connected to server');
}); });
socket.on('chat message', (msg) => { socket.on('chat message', (msg, serverOffset) => {
console.log('Received message:', msg); console.log('Received message:', msg);
const { username, content, recipient } = msg; const { username, content, recipient } = msg;