2 ;;; Mudsync --- Live hackable MUD
3 ;;; Copyright © 2017 Christopher Allan Webber <cwebber@dustycloud.org>
5 ;;; This file is part of Mudsync.
7 ;;; Mudsync is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or
10 ;;; (at your option) any later version.
12 ;;; Mudsync is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;;; General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with Mudsync. If not, see <http://www.gnu.org/licenses/>.
21 function displayMessage(data, self_sent) {
22 var new_entry = document.createElement("div");
23 var new_text = document.createTextNode(data);
24 var stream_metabox = document.getElementById("stream-metabox");
25 var should_scroll = false;
26 if(stream_metabox.scrollTop === (stream_metabox.scrollHeight
27 - stream_metabox.offsetHeight)) {
31 new_entry.setAttribute("class", "stream-entry self-sent");
32 document.getElementById("main-input").value = "";
34 new_entry.setAttribute("class", "stream-entry");
36 new_entry.appendChild(new_text);
37 document.getElementById("stream").appendChild(new_entry);
39 stream_metabox.scrollTop = stream_metabox.scrollHeight;
43 function installWebsocket() {
44 // TODO: Don't hardcode the websocket path; pull it from the DOM
45 var address = "ws://".concat(window.location.hostname, ":", window.location.port);
46 var ws = new WebSocket(address);
47 ws.onmessage = function(evt) {
48 displayMessage(evt.data, false);
50 ws.onopen = function() {
51 console.log("connected");
53 ws.onclose = function () {
54 console.log("closed websocket");
59 function installUIHooks(ws) {
60 var input = document.getElementById("main-input");
61 input.onkeypress = function(e) {
62 if (!e) e = window.event;
63 var keyCode = e.keyCode || e.which;
64 if (keyCode == '13') {
65 var input_val = input.value;
66 displayMessage("> ".concat(input_val), true);
67 sendMessageToServer(ws, input_val);
72 function sendMessageToServer(ws, data) {
77 window.onload = function () {