adding contacts list
This commit is contained in:
@@ -4,6 +4,8 @@ const recipientForm = document.getElementById('recipientForm');
|
||||
const recipientInput = document.getElementById('recipient');
|
||||
const messages = document.getElementById('messages');
|
||||
const messageBox = document.getElementById('messageBox');
|
||||
const contacts = document.getElementById('contacts');
|
||||
|
||||
let currentRecipient = null;
|
||||
|
||||
window.onload = () => {
|
||||
@@ -62,14 +64,27 @@ async function initializeSocket() {
|
||||
document.getElementById('input').placeholder = `Send message as: ${username}`;
|
||||
})
|
||||
|
||||
// Main socket connection, it is displaying all messages in real time
|
||||
socket.on('chat message', (msg) => {
|
||||
console.log('Received message:', msg);
|
||||
|
||||
const { username, content, recipient } = msg;
|
||||
const item = document.createElement('li');
|
||||
item.textContent = `${username}: ${content}`;
|
||||
messages.appendChild(item);
|
||||
window.scrollTo(0, document.body.scrollHeight);
|
||||
|
||||
// username is sender username!
|
||||
|
||||
// Add and display contact/incoming messages whatever
|
||||
if(recipient !== currentRecipient && username !== currentUsername) {
|
||||
const contact = document.createElement('li');
|
||||
contact.textContent = username;
|
||||
contacts.appendChild(contact);
|
||||
console.log('created contact')
|
||||
}
|
||||
// Display messages
|
||||
if (recipient === currentRecipient || username === currentRecipient) {
|
||||
const item = document.createElement('li');
|
||||
item.textContent = `${username}: ${content}`;
|
||||
messages.appendChild(item);
|
||||
window.scrollTo(0, document.body.scrollHeight);
|
||||
}
|
||||
});
|
||||
|
||||
// If not previous messages found on backend
|
||||
@@ -83,6 +98,8 @@ async function initializeSocket() {
|
||||
console.log('There was an error: ', msg)
|
||||
messageBox.innerHTML = msg;
|
||||
})
|
||||
|
||||
// Recipient form, it request previous messages after submitting
|
||||
recipientForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
if (recipientInput.value) {
|
||||
@@ -90,10 +107,13 @@ async function initializeSocket() {
|
||||
localStorage.setItem('currentRecipient', currentRecipient);
|
||||
messages.innerHTML = '';
|
||||
console.log('Requesting messages for recipient:', currentRecipient);
|
||||
|
||||
// Request previous messages after form submit
|
||||
socket.emit('get messages', currentRecipient);
|
||||
}
|
||||
});
|
||||
|
||||
// Form for sending messages
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
if (input.value && currentRecipient) {
|
||||
@@ -102,6 +122,8 @@ async function initializeSocket() {
|
||||
input.value = '';
|
||||
}
|
||||
});
|
||||
|
||||
// Socket connection for getting previous messages
|
||||
socket.on('messages history', (messagesHistory) => {
|
||||
console.log('Received messages history:', messagesHistory);
|
||||
messages.innerHTML = ''; // Clear previous messages
|
||||
|
||||
@@ -9,9 +9,8 @@
|
||||
<link rel="stylesheet" href="/static/stylesheet/chat.css">
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<ul id="contacts"></ul>
|
||||
</div>
|
||||
|
||||
<ul id="contacts"></ul>
|
||||
|
||||
<form id="recipientForm">
|
||||
<input id="recipient" autocomplete="off" placeholder="Enter recipient"/>
|
||||
|
||||
@@ -131,4 +131,8 @@ p {
|
||||
|
||||
#messageBox {
|
||||
color: red;
|
||||
}
|
||||
|
||||
#contacts > li {
|
||||
color: white;
|
||||
}
|
||||
Reference in New Issue
Block a user