index.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta
  6. name="viewport"
  7. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
  8. />
  9. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  10. <title>Not Telegram</title>
  11. <style>
  12. body {
  13. background-color: aqua;
  14. margin: 0;
  15. }
  16. .logo {
  17. background-color: blueviolet;
  18. }
  19. h1 {
  20. text-align: center;
  21. color: aqua;
  22. height: 60px;
  23. margin: 0;
  24. padding-top: 20px;
  25. }
  26. input {
  27. padding-left: 10px;
  28. font-size: 20px;
  29. width: 500px;
  30. height: 50px;
  31. }
  32. #chat {
  33. padding-left: 20px;
  34. font-size: 25px;
  35. color: aqua;
  36. width: 1250px;
  37. height: 600px;
  38. background-color: blueviolet;
  39. }
  40. .inputBlock {
  41. padding-left: 20px;
  42. }
  43. #button {
  44. margin-top: 30px;
  45. height: 50px;
  46. width: 120px;
  47. background-color: blueviolet;
  48. }
  49. #button:hover {
  50. opacity: 0.8;
  51. }
  52. .buttonText {
  53. margin: 0;
  54. font-size: 20px;
  55. color: aqua;
  56. }
  57. .chat {
  58. max-width: 1250px;
  59. margin: 0 auto;
  60. }
  61. .block {
  62. max-width: 1200px;
  63. border: 1px solid aqua;
  64. border-radius: 2%;
  65. padding: 0 30px;
  66. margin: 7px 20px;
  67. box-shadow: 7px 3px 3px black;
  68. color: aqua;
  69. }
  70. #container{
  71. padding-top: 15px;
  72. background-color: blueviolet;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div class="chat">
  78. <div class="logo">
  79. <h1>Чат "Not Telegram"</h1>
  80. </div>
  81. <div class="">
  82. <div class="inputBlock">
  83. <h2>Ваш ник:</h2>
  84. <input
  85. type="text"
  86. name="nickname"
  87. id="nick"
  88. value="illiaKozyr"
  89. />
  90. <h2>Ваше сообщение:</h2>
  91. <input
  92. type="text"
  93. name=""
  94. id="message"
  95. value="test message"
  96. />
  97. <br />
  98. <button id="button">
  99. <p class="buttonText">Отправить</p>
  100. </button>
  101. <h2>Чат:</h2>
  102. </div>
  103. <div id="container"></div>
  104. </div>
  105. </div>
  106. <script>
  107. const url = "http://students.a-level.com.ua:10012";
  108. const nick = document.getElementById("nick");
  109. const message = document.getElementById("message");
  110. const btnSendMessage = document.getElementById("button");
  111. const container = document.getElementById("container");
  112. let nextMessageId = 1;
  113. let delay = 2000;
  114. setInterval(getMessages, delay);
  115. function getMessages() {
  116. jsonPost(url, { func: "getMessages", messageId: nextMessageId })
  117. .then((response) => response.json())
  118. .then((data) => {
  119. nextMessageId = data.nextMessageId;
  120. updateMessages(data.data);
  121. });
  122. }
  123. function jsonPost(url, data) {
  124. return fetch(url, {
  125. method: "POST",
  126. body: JSON.stringify(data),
  127. });
  128. }
  129. function updateMessages(messageList) {
  130. messageList.forEach((msg) => {
  131. newGotMessage(msg);
  132. });
  133. }
  134. function newGotMessage({ nick, message, timestamp }) {
  135. const newBlock = document.createElement("div");
  136. newBlock.classList.add("block");
  137. const time = new Date(timestamp);
  138. let html = "";
  139. html += `<p>${nick}<p>`;
  140. html += `<p class="message">"${message}"</p>`;
  141. html += `<p>${time.toString().substring(16, 24)}</p>`;
  142. newBlock.innerHTML = html;
  143. container.prepend(newBlock);
  144. }
  145. btnSendMessage.addEventListener("click", sendAndCheck);
  146. async function sendAndCheck() {
  147. await sendMessage();
  148. getMessages();
  149. message.value = "";
  150. }
  151. async function sendMessage() {
  152. jsonPost(url, {
  153. func: "addMessage",
  154. nick: nick.value,
  155. message: message.value,
  156. });
  157. }
  158. </script>
  159. </body>
  160. </html>