diff --git a/chatserver/server.py b/chatserver/server.py index 94d79ad..8da9166 100644 --- a/chatserver/server.py +++ b/chatserver/server.py @@ -209,9 +209,9 @@ def get_history(): """Retrieve message history for a specific chat room.""" chat_room = request.args.get('chat_room', 'default') if chat_room not in message_history: - return jsonify({"error": f"No history found for chat room '{chat_room}'"}), 404 + return jsonify({"messages": []}) # Return an empty list if no history exists - return jsonify({"chat_room": chat_room, "messages": message_history[chat_room]}), 200 + return jsonify({"messages": message_history[chat_room]}) # WebSocket event handlers @socketio.on('connect') diff --git a/chatserver/templates/index.html b/chatserver/templates/index.html index 81fd396..8c59b08 100644 --- a/chatserver/templates/index.html +++ b/chatserver/templates/index.html @@ -235,10 +235,15 @@ chatDiv.innerHTML = ""; // Clear the chat history // Fetch message history for the new chat room - fetch(`/get_history?chat_room=${chatRoom}`) - .then(response => response.json()) + fetch(`/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) { data.messages.forEach(message => { const messageElement = document.createElement('div'); messageElement.textContent = message;