From 966aa6a2f9cfe290507b2770fbdaa579c9194a20 Mon Sep 17 00:00:00 2001 From: gounux Date: Sun, 21 Jul 2024 10:21:54 +0200 Subject: [PATCH] add more checks in the ws test webpage --- gischat/ws_html.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/gischat/ws_html.py b/gischat/ws_html.py index 3692efc..365f3ec 100755 --- a/gischat/ws_html.py +++ b/gischat/ws_html.py @@ -12,7 +12,7 @@
- + @@ -33,25 +33,34 @@ message.appendChild(content); messages.appendChild(message); }; - function connect(event) { + function connect(event){ const instance = document.getElementById("instance"); const ssl = document.getElementById("ssl"); const room = document.getElementById("roomId"); const author = document.getElementById("authorId"); - const ws_protocol = ssl.value ? "wss" : "ws"; - websocket = new WebSocket(`${ws_protocol}://${instance.value}/room/${room.value}/ws`); + const ws_protocol = ssl.checked ? "wss" : "ws"; + const ws_url = `${ws_protocol}://${instance.value}/room/${room.value}/ws`; + console.log(`Connecting websocket at url ${ws_url}`); + websocket = new WebSocket(ws_url); websocket.onopen = (event) => displayMessage("connection to websocket ok"); websocket.onmessage = (event) => { const data = JSON.parse(event.data); const log = `[${data.author}] (${new Date().toLocaleTimeString()}): ${data.message}`; displayMessage(log); }; - websocket.onerror = (error) => displayMessage(error); + websocket.onerror = (error) => { + displayMessage(`Websocket error ${JSON.stringify(error)}`); + console.log("Websocket error", error); + }; event.preventDefault(); }; - function sendMessage(event) { + function sendMessage(event){ const message = document.getElementById("messageText"); const author = document.getElementById("authorId"); + if (!message.value || !author.value){ + alert("Author and message can not be empty"); + return; + } websocket.send(JSON.stringify({message: message.value, author: author.value})); message.value = '' event.preventDefault()