summaryrefslogtreecommitdiffstats
path: root/src/index.ts
blob: b074be1bc9c7b3309e758de7d32011f03e9b6617 (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
type Message = {
  text: string;
  user: string;
  color: string;
};
const url = "http://localhost:5000/";
const input = document.getElementById("msg") as HTMLInputElement;
let username: string;
let color: string;
color = "#123456";
// @ts-ignore
const chatListScroll = $("#chatlist").tinyscrollbar();
function init() {
  const username = prompt("Gimme your name: ");
  if (username === null) {
    document.body.innerHTML = "You should give me your name!";
    return;
  }
  // @ts-ignore
  // chatList.tinyscrollbar();
  //   //@ts-ignore
  //   $("#emot").emoticonize();
  if (input === null) return;
  input.onclick = sendMessage;
  poll();
}
function poll() {
  $.ajax({
    url: url + "poll",
    success: addMessage /* data => {
      console.log(data); // { text: "Some data" } -> will be printed in your browser console every 5 seconds
      poll();
    } */,
    error: () => setTimeout(() => poll(), 1000),
    timeout: 30000, // 30 seconds
  });
}
function sendMessage() {
  const msg = {
    text: input.value,
    user: username,
    color: color,
  };
  fetch(url + "send", {
    method: "POST",
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
    },
    body: JSON.stringify(msg),
  });
}
function addMessage(msg: Message) {
  const board = $(".overview")[0];
  const div = document.createElement("div");
  div.innerHTML = JSON.stringify(msg);
  board.appendChild(div);
  console.log(msg);
  // @ts-ignore
  $(chatListScroll).data().plugin_tinyscrollbar.update("relative");
  // $(chatListScroll).data().plugin_tinyscrollbar.update("bottom");
  poll();
}
init();