diff --git a/chatserver/templates/index.html b/chatserver/templates/index.html index 8c59b08..b95f068 100644 --- a/chatserver/templates/index.html +++ b/chatserver/templates/index.html @@ -227,12 +227,19 @@ function changeChatRoom() { const newChatRoom = prompt("Enter the new chat room:"); if (newChatRoom && newChatRoom.trim() !== "") { + // Leave the current chat room socket.emit('leave', { username: username, chat_room: chatRoom }); + + // Update the chat room chatRoom = newChatRoom.trim(); currentChatRoomElement.textContent = chatRoom; console.log(`Switched to chat room: ${chatRoom}`); + + // Join the new chat room socket.emit('join', { username: username, chat_room: chatRoom }); - chatDiv.innerHTML = ""; // Clear the chat history + + // Clear the chat history in the UI + chatDiv.innerHTML = ""; // Fetch message history for the new chat room fetch(`/get_history?chat_room=${encodeURIComponent(chatRoom)}`) @@ -244,6 +251,7 @@ }) .then(data => { if (data.messages && data.messages.length > 0) { + // Display each message in the chat history data.messages.forEach(message => { const messageElement = document.createElement('div'); messageElement.textContent = message;