mudsync.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. ;;; Mudsync --- Live hackable MUD
  3. ;;; Copyright © 2017 Christine Lemmer-Webber <cwebber@dustycloud.org>
  4. ;;;
  5. ;;; This file is part of Mudsync.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. */
  20. function scrollDown() {
  21. var stream_metabox = document.getElementById("stream-metabox");o
  22. stream_metabox.scrollTop = stream_metabox.scrollHeight;
  23. }
  24. function withMaybeScroll(thunk) {
  25. var stream_metabox = document.getElementById("stream-metabox");
  26. var should_scroll = false;
  27. // if within a reasonable threshold, we scroll
  28. if((stream_metabox.scrollHeight - stream_metabox.offsetHeight)
  29. - stream_metabox.scrollTop <= 50) {
  30. should_scroll = true;
  31. }
  32. thunk();
  33. if (should_scroll) {
  34. stream_metabox.scrollTop = stream_metabox.scrollHeight;
  35. }
  36. }
  37. function displayMessage(data, self_sent) {
  38. var new_entry = document.createElement("div");
  39. withMaybeScroll(
  40. function () {
  41. if (self_sent) {
  42. new_entry.setAttribute("class", "stream-entry self-sent");
  43. document.getElementById("main-input").value = "";
  44. } else {
  45. new_entry.setAttribute("class", "stream-entry");
  46. }
  47. new_entry.innerHTML = data;
  48. document.getElementById("stream").appendChild(new_entry);
  49. });
  50. }
  51. function setConnectedText(string, to_class) {
  52. var stream_metabox = document.getElementById("connection-status");
  53. stream_metabox.textContent = "[" + string + "]";
  54. stream_metabox.setAttribute("class", to_class);
  55. }
  56. function installWebsocket() {
  57. // TODO: Don't hardcode the websocket path; pull it from the DOM
  58. var address = "ws://".concat(window.location.hostname, ":", window.location.port);
  59. var ws = new WebSocket(address);
  60. ws.onmessage = function(evt) {
  61. displayMessage(evt.data, false);
  62. };
  63. ws.onopen = function() {
  64. setConnectedText("connected", "connected");
  65. console.log("connected");
  66. };
  67. ws.onclose = function () {
  68. setConnectedText("disconnected", "disconnected");
  69. // kludge, we shouldn't be using self_sent like this because it
  70. // wipes the input
  71. displayMessage(
  72. "* You have been disconnected. Refresh to (hopefully) reconnect.",
  73. true);
  74. console.log("closed websocket");
  75. };
  76. installUIHooks(ws);
  77. }
  78. function installUIHooks(ws) {
  79. var input = document.getElementById("main-input");
  80. input.onkeypress = function(e) {
  81. if (!e) e = window.event;
  82. var keyCode = e.keyCode || e.which;
  83. if (keyCode == '13') {
  84. var input_val = input.value;
  85. withMaybeScroll(
  86. function () {
  87. displayMessage("> ".concat(input_val), true);
  88. });
  89. sendMessageToServer(ws, input_val);
  90. }
  91. }
  92. }
  93. function sendMessageToServer(ws, data) {
  94. ws.send(data);
  95. }
  96. window.onload = function () {
  97. installWebsocket();
  98. window.onresize = scrollDown;
  99. }