fixed messages displaying multiple times
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
Web chat (not finished!)
|
||||
========================
|
||||
|
||||
Project for hackclub arcade.
|
||||
Web chat application using Express.js, Socket.io, bcrypt, Postgresql, and JWT for user authentication .
|
||||
|
||||
Selfhost:
|
||||
|
||||
@@ -81,7 +81,8 @@ function initializeSocket(server) {
|
||||
|
||||
for (const row of result.rows) {
|
||||
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) {
|
||||
@@ -100,10 +101,6 @@ function initializeSocket(server) {
|
||||
`;
|
||||
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) {
|
||||
//const { username: sender, recipient: receiver, content: content, id: id } = result.rows;
|
||||
console.log('Sending messages:', result.rows);
|
||||
|
||||
@@ -50,7 +50,7 @@ async function initializeSocket() {
|
||||
console.log('Connected to server');
|
||||
});
|
||||
|
||||
socket.on('chat message', (msg) => {
|
||||
socket.on('chat message', (msg, serverOffset) => {
|
||||
console.log('Received message:', msg);
|
||||
|
||||
const { username, content, recipient } = msg;
|
||||
|
||||
Reference in New Issue
Block a user