2 ;;; Mudsync --- Live hackable MUD
3 ;;; Copyright © 2017 Christine Lemmer-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 scrollDown() {
22 var stream_metabox = document.getElementById("stream-metabox");o
23 stream_metabox.scrollTop = stream_metabox.scrollHeight;
26 function withMaybeScroll(thunk) {
27 var stream_metabox = document.getElementById("stream-metabox");
28 var should_scroll = false;
29 // if within a reasonable threshold, we scroll
30 if((stream_metabox.scrollHeight - stream_metabox.offsetHeight)
31 - stream_metabox.scrollTop <= 50) {
36 stream_metabox.scrollTop = stream_metabox.scrollHeight;
41 function displayMessage(data, self_sent) {
42 var new_entry = document.createElement("div");
46 new_entry.setAttribute("class", "stream-entry self-sent");
47 document.getElementById("main-input").value = "";
49 new_entry.setAttribute("class", "stream-entry");
51 new_entry.innerHTML = data;
52 document.getElementById("stream").appendChild(new_entry);
56 function setConnectedText(string, to_class) {
57 var stream_metabox = document.getElementById("connection-status");
58 stream_metabox.textContent = "[" + string + "]";
59 stream_metabox.setAttribute("class", to_class);
62 function installWebsocket() {
63 // TODO: Don't hardcode the websocket path; pull it from the DOM
64 var address = "ws://".concat(window.location.hostname, ":", window.location.port);
65 var ws = new WebSocket(address);
66 ws.onmessage = function(evt) {
67 displayMessage(evt.data, false);
69 ws.onopen = function() {
70 setConnectedText("connected", "connected");
71 console.log("connected");
73 ws.onclose = function () {
74 setConnectedText("disconnected", "disconnected");
75 // kludge, we shouldn't be using self_sent like this because it
78 "* You have been disconnected. Refresh to (hopefully) reconnect.",
80 console.log("closed websocket");
85 function installUIHooks(ws) {
86 var input = document.getElementById("main-input");
87 input.onkeypress = function(e) {
88 if (!e) e = window.event;
89 var keyCode = e.keyCode || e.which;
90 if (keyCode == '13') {
91 var input_val = input.value;
94 displayMessage("> ".concat(input_val), true);
96 sendMessageToServer(ws, input_val);
101 function sendMessageToServer(ws, data) {
105 window.onload = function () {
107 window.onresize = scrollDown;