index.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <style>
  11. .error {
  12. background: rgb(245, 124, 124);
  13. }
  14. .messageWrapper {
  15. border-top: 1px solid black;
  16. padding: 15px;
  17. margin-top: 10px;
  18. }
  19. .messageNick {
  20. font-weight: bold;
  21. }
  22. .messageText {
  23. margin-top: 7px;
  24. }
  25. .messageDate {
  26. margin-top: 7px;
  27. width: 100%;
  28. text-align: right;
  29. }
  30. </style>
  31. <div>
  32. <input type="text" id="nameInput" placeholder="name" />
  33. <input type="text" id="textInput" placeholder="message" />
  34. <button id="submitButton">Submit</button>
  35. <div id="messageHistory"></div>
  36. </div>
  37. <script>
  38. const delay = (ms) => new Promise((ok) => setTimeout(() => ok(ms), ms));
  39. function jsonPost(url, data) {
  40. return new Promise((resolve, reject) => {
  41. fetch(url, {
  42. method: "POST",
  43. // headers: { "Content-Type": "text/json" },
  44. body: JSON.stringify(data),
  45. }).then(
  46. (res) => {
  47. resolve(res.json());
  48. },
  49. (err) => {
  50. reject(new Error(err));
  51. }
  52. );
  53. });
  54. }
  55. let nextMessageId = 0;
  56. const drawMessages = (parent, elements) => {
  57. for (let message of elements) {
  58. let messageWrapper = document.createElement("div");
  59. let messageText = document.createElement("div");
  60. let messageNick = document.createElement("div");
  61. let messageDate = document.createElement("div");
  62. messageWrapper.append(messageNick);
  63. messageWrapper.append(messageText);
  64. messageWrapper.append(messageDate);
  65. messageNick.innerText = message.nick;
  66. messageText.innerText = message.message;
  67. messageDate.innerText = new Date(+message.timestamp).toLocaleDateString();
  68. messageWrapper.classList.add("messageWrapper");
  69. messageNick.classList.add("messageNick");
  70. messageText.classList.add("messageText");
  71. messageDate.classList.add("messageDate");
  72. parent.prepend(messageWrapper);
  73. }
  74. };
  75. submitButton.addEventListener("click", function sendMessage() {
  76. let name = nameInput.value.trim();
  77. let text = textInput.value.trim();
  78. if (name.length === 0) {
  79. nameInput.classList.add("error");
  80. return;
  81. } else {
  82. nameInput.classList.remove("error");
  83. }
  84. if (text.length === 0) {
  85. textInput.classList.add("error");
  86. return;
  87. } else {
  88. textInput.classList.remove("error");
  89. }
  90. sendAndCheck(name, text);
  91. });
  92. async function sendMessage(nick, message) {
  93. let nextMessageId = await jsonPost("http://students.a-level.com.ua:10012", {
  94. func: "addMessage",
  95. nick: nick,
  96. message: message,
  97. });
  98. return nextMessageId;
  99. }
  100. // let getMessagesPoll = setInterval(() => {
  101. // jsonPost("http://students.a-level.com.ua:10012", {
  102. // func: "getMessages",
  103. // messageId: nextMessageId,
  104. // }).then((res) => {
  105. // if (nextMessageId !== res.nextMessageId) {
  106. // drawMessages(messageHistory, res.data);
  107. // nextMessageId = res.nextMessageId;
  108. // }
  109. // console.log(res.data);
  110. // console.log(nextMessageId);
  111. // });
  112. // }, 3000);
  113. async function getMessages(nextMessageId) {
  114. let response = await jsonPost("http://students.a-level.com.ua:10012", {
  115. func: "getMessages",
  116. messageId: nextMessageId,
  117. });
  118. if (nextMessageId < response.nextMessageId) {
  119. drawMessages(messageHistory, response.data);
  120. nextMessageId = response.nextMessageId;
  121. }
  122. return nextMessageId;
  123. }
  124. async function sendAndCheck(nick, message) {
  125. let nextMessageId = await sendMessage(nick, message);
  126. getMessages(nextMessageId);
  127. }
  128. async function checkLoop() {
  129. while (true) {
  130. await delay(3000);
  131. nextMessageId = await getMessages(nextMessageId);
  132. }
  133. }
  134. checkLoop();
  135. </script>
  136. </body>
  137. </html>