49 lines
1.4 KiB
HTML
49 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Chatroom</title>
|
|
<style>
|
|
/* Your CSS styles here */
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="chatbox"></div>
|
|
<input type="text" id="message" placeholder="Type your message">
|
|
<button id="send">Send</button>
|
|
|
|
<!-- Include the Socket.io script from a CDN -->
|
|
<script src="https://cdn.socket.io/4.0.1/socket.io.js"></script>
|
|
|
|
<script>
|
|
const chatbox = document.getElementById('chatbox');
|
|
const messageInput = document.getElementById('message');
|
|
const sendButton = document.getElementById('send');
|
|
|
|
// Connect to the Socket.io server
|
|
const socket = io();
|
|
|
|
sendButton.addEventListener('click', function() {
|
|
const message = messageInput.value;
|
|
if (message.trim() !== '') {
|
|
socket.emit('message', message); // Send the message to the server
|
|
messageInput.value = '';
|
|
}
|
|
});
|
|
|
|
socket.on('message', function(message) {
|
|
const messageElement = document.createElement('div');
|
|
messageElement.textContent = message;
|
|
chatbox.appendChild(messageElement);
|
|
chatbox.scrollTop = chatbox.scrollHeight; // Scroll to the bottom
|
|
});
|
|
|
|
messageInput.addEventListener('keyup', function(event) {
|
|
if (event.key === 'Enter') {
|
|
sendButton.click();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|