diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-24 22:25:00 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2024-01-24 22:25:00 +0100 |
commit | df4b62f2a1aa616e4038b1511b2e38cd956e6423 (patch) | |
tree | 7a320e08c3a6f3f9b2f944f9158a728c582efc16 /static/chat.js | |
parent | 67441f813917d0a9c6624ec04ddc08d89ec2b3bc (diff) | |
download | cats-radio-node-df4b62f2a1aa616e4038b1511b2e38cd956e6423.tar.gz cats-radio-node-df4b62f2a1aa616e4038b1511b2e38cd956e6423.tar.bz2 cats-radio-node-df4b62f2a1aa616e4038b1511b2e38cd956e6423.zip |
Get Chat window working
Diffstat (limited to 'static/chat.js')
-rw-r--r-- | static/chat.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/static/chat.js b/static/chat.js new file mode 100644 index 0000000..00c4d4e --- /dev/null +++ b/static/chat.js @@ -0,0 +1,102 @@ +var socket = null; +var retry_scheduled = false; + +function init_socket() { + + retry_scheduled = false; + + if (socket !== null) { + socket.onmessage = null; + socket.onopen = null; + socket.onclose = null; + socket.onerror = null; + + delete socket; + } + + socket = new WebSocket("ws://" + window.location.host + "/chat/ws"); + + socket.onmessage = function(data) { + const message = JSON.parse(data.data) + add_message(message); + } + + socket.onopen = function(_data) { + console.log("Websocket open"); + } + + socket.onclose = function(_code, text) { + if (!retry_scheduled) { + console.log(`Websocket closed ${text}`); + retry_scheduled = true; + init_socket(); + } + } + + socket.onerror = function(e) { + if (!retry_scheduled) { + console.log(`Websocket error because ${e}. Trying again in 3s`); + retry_scheduled = true; + setTimeout(init_socket, 3000); + } + } +} + +function keep_alive() { + if (socket !== null && socket.readyState == 1) { + try { + socket.send('{}'); + } catch (e) { + } + } + + setTimeout(keep_alive, 10000); +} + +function add_message(message) { + let template = document.getElementById('message_template'); + let clon = template.content.cloneNode(true); + + const msg_timestamp = clon.querySelector("div.msg_timestamp"); + const ts = strftime("%Y-%m-%d %H:%M:%S", new Date(message.received_at * 1000)); + msg_timestamp.textContent = `${ts} UTC`; + + const msg_from = clon.querySelector("div.msg_from"); + msg_from.textContent = `${message.from_callsign}-${message.from_ssid}`; + + const msg_comment = clon.querySelector("div.msg_comment"); + msg_comment.textContent = message.comment; + + const messagelist = document.getElementById('messagelist'); + messagelist.appendChild(clon); + messagelist.scrollTo(0, messagelist.scrollHeight); +} + +function call_clicked(element_clicked) { + let call_ssid = element_clicked.textContent; + document.getElementById('dest').value = call_ssid; +} + +async function btn_chat_send_message() { + let data = { + 'comment': document.getElementById('whisker_comment').value, + 'destinations': [], + }; + + let callsign_ssid = document.getElementById('dest').value; + if (callsign_ssid !== "") { + let splitted = callsign_ssid.split("-"); + let ssid = 0; + if (splitted.length == 2) { + ssid = parseInt(splitted[1], 10); + } + data.destinations.push({'callsign': splitted[0], 'ssid': ssid}); + } + await post('/api/send_packet', data); +} + +window.addEventListener("load", (_event) => { + init_socket(); + keep_alive(); +}); + |