67 lines
2.1 KiB
HTML
67 lines
2.1 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Chat Client</title>
|
|
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
|
|
</head>
|
|
<body>
|
|
<h1>Chat Client</h1>
|
|
<button onclick="registerClient()">Register Client</button>
|
|
<div>
|
|
<input type="text" id="message" placeholder="Enter your message">
|
|
<button onclick="sendMessage()">Send Message</button>
|
|
</div>
|
|
<div id="messages"></div>
|
|
|
|
<script>
|
|
let socket;
|
|
|
|
async function registerClient() {
|
|
const tenantId = '1';
|
|
const apiKey = 'EveAI-CHAT-8553-7987-2800-9115-6454';
|
|
|
|
const response = await fetch('http://127.0.0.1:5001/chat/register_client', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ tenant_id: tenantId, api_key: apiKey })
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
const token = data.token;
|
|
|
|
socket = io('http://127.0.0.1:5001/chat', {
|
|
auth: {
|
|
token: `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
console.log('Connected to server');
|
|
});
|
|
|
|
socket.on('response', (msg) => {
|
|
const messagesDiv = document.getElementById('messages');
|
|
const messageElement = document.createElement('div');
|
|
messageElement.innerText = `Response: ${msg.data}`;
|
|
messagesDiv.appendChild(messageElement);
|
|
});
|
|
} else {
|
|
console.error('Registration failed');
|
|
}
|
|
}
|
|
|
|
function sendMessage() {
|
|
const message = document.getElementById('message').value;
|
|
if (socket) {
|
|
socket.emit('message', { content: message });
|
|
} else {
|
|
console.error('Socket not connected');
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|