gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
<!DOCTYPE html> <html> <head> <script type="text/javascript"…
- User
- Assistant
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
}
#messages {
height: 300px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
border-radius: 4px;
margin-bottom: 10px;
}
.message {
margin: 5px 0;
}
input[type="text"] {
width: calc(100% - 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
padding: 10px 15px;
border: none;
background-color: #28a745;
color: white;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="container">
<h1>Multiplayer Chat</h1>
<div id="messages"></div>
<input type="text" id="messageInput" placeholder="Type your message here..." />
<input type="submit" id="sendButton" value="Send" />
</div>
<script>
const drone = new ScaleDrone('YOUR_CHANNEL_ID');
const room = drone.subscribe('observable-room');
room.on('open', error => {
if (error) {
return console.error(error);
}
console.log('Successfully joined the room');
});
room.on('message', (message) => {
const messagesDiv = document.getElementById('messages');
messagesDiv.innerHTML += `<div class="message"><strong>${message.member.clientData.username}: </strong>${message.data}</div>`;
messagesDiv.scrollTop = messagesDiv.scrollHeight; // Auto-scroll to the bottom
});
document.getElementById('sendButton').addEventListener('click', () => {
const input = document.getElementById('messageInput');
const message = input.value;
if (message) {
drone.publish({
room: 'observable-room',
message: message,
});
input.value = ''; // Clear the input field
}
});
// Optional: Allow sending messages with Enter key
document.getElementById('messageInput').addEventListener('keypress', (event) => {
if (event.key === 'Enter') {
document.getElementById('sendButton').click();
}
});
</script>
</body>
</html>
HTML Structure: The basic structure includes a header, a message display area, and input fields for sending messages.
Styling: CSS provides a clean and user-friendly interface allowing for easy readability and interaction.
ScaleDrone Integration: This JavaScript code connects to a ScaleDrone channel for real-time messaging. Replace 'YOUR_CHANNEL_ID' with your actual ScaleDrone channel ID.
Dynamic Message Handling: The message area updates dynamically as new messages arrive, and the interface allows for message sending either via button click or the Enter key.
This implementation provides a simple yet complete multiplayer chat application.
Not yet included in a published release.