diff --git a/chatserver/templates/index.html b/chatserver/templates/index.html index b95f068..b2dcb17 100644 --- a/chatserver/templates/index.html +++ b/chatserver/templates/index.html @@ -160,10 +160,17 @@ socket.emit('join', { username: username, chat_room: chatRoom }); // Fetch message history for the initial chat room - fetch(`/get_history?chat_room=${chatRoom}`) - .then(response => response.json()) + const serverBaseUrl = `${window.location.protocol}//${window.location.hostname}:5000`; + fetch(`${serverBaseUrl}/get_history?chat_room=${encodeURIComponent(chatRoom)}`) + .then(response => { + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.json(); + }) .then(data => { - if (data.messages) { + 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;