Spaces:
Sleeping
Sleeping
Update static/index.html
Browse files- static/index.html +38 -29
static/index.html
CHANGED
|
@@ -2,49 +2,57 @@
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>Chat
|
| 6 |
<script>
|
| 7 |
-
|
| 8 |
-
try {
|
| 9 |
-
const response = await fetch('/custom-auth');
|
| 10 |
-
const data = await response.json();
|
| 11 |
-
document.getElementById('authToken').textContent = `Auth Token: ${data.token}`;
|
| 12 |
-
} catch (error) {
|
| 13 |
-
console.error('Error:', error);
|
| 14 |
-
}
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
async function sendMessage() {
|
| 18 |
const inputElement = document.getElementById('messageInput');
|
| 19 |
-
const
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
// Display the user's message
|
| 23 |
displayMessage(`You: ${message}`, 'user');
|
| 24 |
|
| 25 |
-
//
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
// Clear the input field
|
| 32 |
inputElement.value = '';
|
| 33 |
}
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
const messageElement = document.createElement('p');
|
| 38 |
messageElement.textContent = text;
|
| 39 |
-
messageElement.className =
|
| 40 |
-
|
| 41 |
}
|
| 42 |
|
| 43 |
-
//
|
| 44 |
-
document.addEventListener('DOMContentLoaded',
|
| 45 |
-
document.getElementById('messageInput')
|
|
|
|
| 46 |
if (event.key === 'Enter') {
|
| 47 |
sendMessage();
|
|
|
|
| 48 |
}
|
| 49 |
});
|
| 50 |
});
|
|
@@ -52,18 +60,19 @@
|
|
| 52 |
<style>
|
| 53 |
.user { color: blue; }
|
| 54 |
.server { color: green; }
|
|
|
|
| 55 |
</style>
|
| 56 |
</head>
|
| 57 |
-
<body
|
| 58 |
-
<h1>
|
| 59 |
-
<div id="
|
| 60 |
-
<div id="messages" style="height: 200px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px;"></div>
|
| 61 |
<input type="text" id="messageInput" placeholder="Type your message here and press Enter">
|
| 62 |
</body>
|
| 63 |
</html>
|
| 64 |
|
| 65 |
|
| 66 |
|
|
|
|
| 67 |
<!-- ################################################################################################## -->
|
| 68 |
<!-- <!DOCTYPE html>
|
| 69 |
<html lang="en">
|
|
|
|
| 2 |
<html lang="en">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>Chat Interface</title>
|
| 6 |
<script>
|
| 7 |
+
// Function to handle sending messages and receiving responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
async function sendMessage() {
|
| 9 |
const inputElement = document.getElementById('messageInput');
|
| 10 |
+
const messagesContainer = document.getElementById('messages');
|
| 11 |
+
const message = inputElement.value.trim();
|
| 12 |
+
|
| 13 |
+
// Prevent sending empty messages
|
| 14 |
+
if (!message) return;
|
| 15 |
|
| 16 |
// Display the user's message
|
| 17 |
displayMessage(`You: ${message}`, 'user');
|
| 18 |
|
| 19 |
+
// Assuming the same '/custom-auth' endpoint is used for messages
|
| 20 |
+
try {
|
| 21 |
+
const response = await fetch('/custom-auth', {
|
| 22 |
+
method: 'POST', // Assuming you adjust your backend to accept POST for chat messages
|
| 23 |
+
headers: {
|
| 24 |
+
'Content-Type': 'application/json',
|
| 25 |
+
},
|
| 26 |
+
body: JSON.stringify({ message }),
|
| 27 |
+
});
|
| 28 |
+
const data = await response.json();
|
| 29 |
+
|
| 30 |
+
// Display the received response
|
| 31 |
+
displayMessage(`Server: ${data.response}`, 'server'); // Adjust 'data.response' based on your actual response structure
|
| 32 |
+
} catch (error) {
|
| 33 |
+
console.error('Error:', error);
|
| 34 |
+
displayMessage('Error sending message.', 'error');
|
| 35 |
+
}
|
| 36 |
|
| 37 |
// Clear the input field
|
| 38 |
inputElement.value = '';
|
| 39 |
}
|
| 40 |
|
| 41 |
+
// Function to display messages
|
| 42 |
+
function displayMessage(text, className) {
|
| 43 |
const messageElement = document.createElement('p');
|
| 44 |
messageElement.textContent = text;
|
| 45 |
+
messageElement.className = className;
|
| 46 |
+
document.getElementById('messages').appendChild(messageElement);
|
| 47 |
}
|
| 48 |
|
| 49 |
+
// Attach event listener to input field for the Enter key
|
| 50 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 51 |
+
const inputElement = document.getElementById('messageInput');
|
| 52 |
+
inputElement.addEventListener('keydown', (event) => {
|
| 53 |
if (event.key === 'Enter') {
|
| 54 |
sendMessage();
|
| 55 |
+
event.preventDefault(); // Prevent the default action to stop form submission
|
| 56 |
}
|
| 57 |
});
|
| 58 |
});
|
|
|
|
| 60 |
<style>
|
| 61 |
.user { color: blue; }
|
| 62 |
.server { color: green; }
|
| 63 |
+
.error { color: red; }
|
| 64 |
</style>
|
| 65 |
</head>
|
| 66 |
+
<body>
|
| 67 |
+
<h1>Chat with Server</h1>
|
| 68 |
+
<div id="messages" style="height: 300px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px;"></div>
|
|
|
|
| 69 |
<input type="text" id="messageInput" placeholder="Type your message here and press Enter">
|
| 70 |
</body>
|
| 71 |
</html>
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
+
|
| 76 |
<!-- ################################################################################################## -->
|
| 77 |
<!-- <!DOCTYPE html>
|
| 78 |
<html lang="en">
|