From: Christopher Allan Webber Date: Sat, 21 Jan 2017 04:06:01 +0000 (-0600) Subject: sending messages to the server from websockets works X-Git-Tag: fosdem-2017~107 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=6991f02ec37a212591ea79a2196a74c3666362bf;p=mudsync.git sending messages to the server from websockets works --- diff --git a/data/web-static/js/mudsync.js b/data/web-static/js/mudsync.js index f66a947..1a08b47 100644 --- a/data/web-static/js/mudsync.js +++ b/data/web-static/js/mudsync.js @@ -20,10 +20,20 @@ function displayMessage(data) { var new_entry = document.createElement("div"); - new_entry.setAttribute("class", "stream-entry"); var new_text = document.createTextNode(data); + var stream_metabox = document.getElementById("stream-metabox"); + var should_scroll = false; + if(stream_metabox.scrollTop === (stream_metabox.scrollHeight + - stream_metabox.offsetHeight)) { + should_scroll = true; + } + document.getElementById("main-input").value = ""; + new_entry.setAttribute("class", "stream-entry"); new_entry.appendChild(new_text); document.getElementById("stream").appendChild(new_entry); + if (should_scroll) { + stream_metabox.scrollTop = stream_metabox.scrollHeight; + } } function installWebsocket() { @@ -39,8 +49,25 @@ function installWebsocket() { ws.onclose = function () { console.log("closed websocket"); }; + installUIHooks(ws); +} + +function installUIHooks(ws) { + var input = document.getElementById("main-input"); + input.onkeypress = function(e) { + if (!e) e = window.event; + var keyCode = e.keyCode || e.which; + if (keyCode == '13') { + sendMessageToServer(ws, input.value); + } + } } +function sendMessageToServer(ws, data) { + ws.send(data); +} + + window.onload = function () { installWebsocket(); }