From f73ad3414001238592f58b3b0514ba723afa84d2 Mon Sep 17 00:00:00 2001 From: Dangrainage <99558179+Dangrainage@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:11:13 +0200 Subject: [PATCH] Update chatroom.HTML --- chatroom.HTML | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/chatroom.HTML b/chatroom.HTML index fa6a66c..b828ad4 100644 --- a/chatroom.HTML +++ b/chatroom.HTML @@ -24,6 +24,11 @@ // Create a WebSocket connection using the Echo Test Service 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 socket.addEventListener('message', event => { const message = event.data; @@ -36,11 +41,13 @@ const message = messageInput.value; if (message.trim() === '') return; - // Send the message to the server - socket.send(message); + if (socket.readyState === WebSocket.OPEN) { + // Send the message to the server + socket.send(message); - // Clear the input field - messageInput.value = ''; + // Clear the input field + messageInput.value = ''; + } } function appendMessageToChatbox(message) {