From 23d82fb1117474bdc1f32d7c9c5d158a7c029403 Mon Sep 17 00:00:00 2001 From: Dangrainage <99558179+Dangrainage@users.noreply.github.com> Date: Mon, 28 Aug 2023 20:24:43 +0200 Subject: [PATCH] Update chatroom.HTML --- chatroom.HTML | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/chatroom.HTML b/chatroom.HTML index 464f4fc..58b26c1 100644 --- a/chatroom.HTML +++ b/chatroom.HTML @@ -39,12 +39,18 @@ sendButton.addEventListener('click', function() { const message = messageInput.value; if (message.trim() !== '') { - messages.push(message); - updateChatbox(); + sendMessage(message); messageInput.value = ''; } }); + function sendMessage(message) { + // Here, you would typically send the message to your server using an AJAX request + // For demonstration purposes, we'll just add the message locally + messages.push(message); + updateChatbox(); + } + function updateChatbox() { chatbox.innerHTML = messages.map(msg => `<div>${msg}</div>`).join(''); chatbox.scrollTop = chatbox.scrollHeight; // Scroll to the bottom @@ -55,6 +61,21 @@ sendButton.click(); } }); + + // Polling with a 3-second interval + setInterval(function() { + // Perform an AJAX request to the server to get new messages + // Replace the URL with the appropriate endpoint on your server + fetch('/getNewMessages') + .then(response => response.json()) + .then(data => { + messages = messages.concat(data.messages); + updateChatbox(); + }) + .catch(error => { + console.error('Error fetching new messages:', error); + }); + }, 3000); // 3 seconds interval </script> </body> </html>