65 lines
1.7 KiB
HTML
65 lines
1.7 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>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
}
|
|
#chatbox {
|
|
width: 400px;
|
|
height: 300px;
|
|
border: 1px solid #ccc;
|
|
padding: 10px;
|
|
overflow: auto;
|
|
}
|
|
#message {
|
|
width: 100%;
|
|
padding: 5px;
|
|
}
|
|
#send {
|
|
margin-top: 5px;
|
|
}
|
|
</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>
|