|
import axios from 'axios'; |
|
|
|
let chatBox = document.getElementById('chatBox'); |
|
let inputField = document.getElementById('inputField'); |
|
|
|
function sendMessage() { |
|
let message = inputField.value; |
|
let messageElement = document.createElement('p'); |
|
messageElement.textContent = 'You: ' + message; |
|
chatBox.appendChild(messageElement); |
|
|
|
axios.post('/api/game', { message: message }) |
|
.then(response => { |
|
let botMessageElement = document.createElement('p'); |
|
botMessageElement.textContent = 'Bot: ' + response.data.message; |
|
chatBox.appendChild(botMessageElement); |
|
}) |
|
.catch(error => console.error(error)); |
|
} |
|
|
|
inputField.addEventListener('keydown', function(event) { |
|
if (event.key === 'Enter') { |
|
event.preventDefault(); |
|
sendMessage(); |
|
inputField.value = ''; |
|
} |
|
}); |
|
|