Update chatroom.HTML
This commit is contained in:
parent
61d7c87fa8
commit
f73ad34140
1 changed files with 11 additions and 4 deletions
|
@ -24,6 +24,11 @@
|
||||||
// Create a WebSocket connection using the Echo Test Service
|
// Create a WebSocket connection using the Echo Test Service
|
||||||
const socket = new WebSocket('wss://echo.websocket.org');
|
const socket = new WebSocket('wss://echo.websocket.org');
|
||||||
|
|
||||||
|
// Handle WebSocket open event
|
||||||
|
socket.addEventListener('open', () => {
|
||||||
|
sendButton.disabled = false; // Enable the send button when the WebSocket is open
|
||||||
|
});
|
||||||
|
|
||||||
// Handle incoming messages from the server
|
// Handle incoming messages from the server
|
||||||
socket.addEventListener('message', event => {
|
socket.addEventListener('message', event => {
|
||||||
const message = event.data;
|
const message = event.data;
|
||||||
|
@ -36,12 +41,14 @@
|
||||||
const message = messageInput.value;
|
const message = messageInput.value;
|
||||||
if (message.trim() === '') return;
|
if (message.trim() === '') return;
|
||||||
|
|
||||||
|
if (socket.readyState === WebSocket.OPEN) {
|
||||||
// Send the message to the server
|
// Send the message to the server
|
||||||
socket.send(message);
|
socket.send(message);
|
||||||
|
|
||||||
// Clear the input field
|
// Clear the input field
|
||||||
messageInput.value = '';
|
messageInput.value = '';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function appendMessageToChatbox(message) {
|
function appendMessageToChatbox(message) {
|
||||||
const messageElement = document.createElement('div');
|
const messageElement = document.createElement('div');
|
||||||
|
|
Loading…
Add table
Reference in a new issue