summaryrefslogtreecommitdiff
path: root/static/js/chat.js
blob: 69c169cc3de3546f1ab2dfe7ccb13f39914562c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(function () {
  const log = document.getElementById("log");
  const form = document.getElementById("chat-form");
  const input = document.getElementById("msg");
  const status = document.getElementById("status");
  const me = document.getElementById("me").textContent.trim();

  let socket;
  let reconnectDelay = 500;

  function el(tag, className, text) {
    const node = document.createElement(tag);
    if (className) node.className = className;
    if (text !== undefined) node.textContent = text;
    return node;
  }

  function atBottom() {
    return log.scrollHeight - log.scrollTop - log.clientHeight < 40;
  }

  function append(node) {
    const stick = atBottom();
    log.appendChild(node);
    if (stick) log.scrollTop = log.scrollHeight;
  }

  function fmtTime(ms) {
    if (!ms) return "";
    const d = new Date(ms);
    return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
  }

  function renderChat(msg) {
    const line = el("div", "chat-line");
    if (msg.nick === me) line.classList.add("mine");
    line.appendChild(el("span", "chat-nick", msg.nick));
    line.appendChild(el("span", "chat-body", " " + msg.body));
    line.appendChild(el("span", "chat-time", " " + fmtTime(msg.at)));
    append(line);
  }

  function renderSystem(msg) {
    append(el("div", "chat-system", msg.body));
  }

  function handle(msg) {
    switch (msg.type) {
      case "history":
        log.innerHTML = "";
        (msg.history || []).forEach(renderChat);
        break;
      case "chat":
        renderChat(msg);
        break;
      case "system":
        renderSystem(msg);
        break;
    }
  }

  function connect() {
    const proto = location.protocol === "https:" ? "wss" : "ws";
    socket = new WebSocket(`${proto}://${location.host}/ws`);

    socket.onopen = function () {
      status.textContent = "connected ✓";
      status.className = "status ok";
      reconnectDelay = 500;
    };

    socket.onmessage = function (event) {
      try {
        handle(JSON.parse(event.data));
      } catch (e) {
        console.error("bad message", e);
      }
    };

    socket.onclose = function () {
      status.textContent = "reconnecting…";
      status.className = "status";
      setTimeout(connect, reconnectDelay);
      reconnectDelay = Math.min(reconnectDelay * 2, 8000);
    };

    socket.onerror = function () {
      socket.close();
    };
  }

  form.addEventListener("submit", function (e) {
    e.preventDefault();
    const body = input.value.trim();
    if (!body || !socket || socket.readyState !== WebSocket.OPEN) return;
    socket.send(JSON.stringify({ type: "chat", body: body }));
    input.value = "";
  });

  connect();
})();