Skip to content

Commit

Permalink
add more checks in the ws test webpage
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 21, 2024
1 parent ea9f0f5 commit 966aa6a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions gischat/ws_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<label>Room: <input type="text" id="roomId" autocomplete="off" value="QGIS"/></label>
<button onclick="connect(event)">Connect</button>
<hr>
<label>Author: <input type="text" id="authorId" autocomplete="off" value="geotribu"/></label>
<label>Author: <input type="text" id="authorId" autocomplete="off" value=""/></label>
<label>Message: <input type="text" id="messageText" autocomplete="off"/></label>
<button>Send</button>
</form>
Expand All @@ -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()
Expand Down

0 comments on commit 966aa6a

Please sign in to comment.