index.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. <div id="container" style="margin: 0 auto; color: red; font-size: 26px; width:400px; background-color: black; ">
  11. <input type="text" id="name" placeholder="name"
  12. style="width: 39%; padding: 12px 20px; background-color: black; color: red; font-size: 16px;">
  13. <input type="text" id="msg" placeholder="message"
  14. style="width: 39%; float: right; padding: 12px 20px; background-color: black; color: red; font-size: 16px;">
  15. <button id="send"
  16. style="width: 100%; display: block; padding: 12px 20px; margin: 0 auto; background-color: black; color: red; font-size: 16px;">Send</button>
  17. <div id="chat" style="margin: 20px"></div>
  18. </div>
  19. <script>
  20. async function jsonPost(url = '', data = {}) {
  21. const response = await fetch(url, {
  22. method: 'POST',
  23. body: JSON.stringify(data)
  24. })
  25. return await response.json()
  26. }
  27. // return new Promise((resolve, reject) => {
  28. // var x = new XMLHttpRequest();
  29. // x.onerror = () => reject(new Error('jsonPost failed'))
  30. // //x.setRequestHeader('Content-Type', 'application/json');
  31. // x.open("POST", url, true);
  32. // x.send(JSON.stringify(data))
  33. // x.onreadystatechange = () => {
  34. // if (x.readyState == XMLHttpRequest.DONE && x.status == 200) {
  35. // resolve(JSON.parse(x.responseText))
  36. // }
  37. // else if (x.status != 200) {
  38. // reject(new Error('status is not 200'))
  39. // }
  40. // }
  41. // })
  42. async function sendMessage(nick, message) {
  43. await jsonPost("http://students.a-level.com.ua:10012", { func: 'addMessage', nick: nick, message: message })
  44. getMessages()
  45. }
  46. let inputName = document.getElementById("name")
  47. let inputMessage = document.getElementById("msg")
  48. let buttonSend = document.getElementById("send")
  49. buttonSend.onclick = () => sendMessage(inputName.value, inputMessage.value)
  50. function showChat(data) {
  51. let div = document.getElementById('chat')
  52. for (let key in data.data) {
  53. let divMessage = document.createElement('div')
  54. divMessage.innerText = `${data.data[key].nick} : ${data.data[key].message}`
  55. divMessage.style.borderBottom = "2px solid red"
  56. div.prepend(divMessage)
  57. let time = document.createElement('div')
  58. time.innerText = `${new Date(data.data[key].timestamp).toLocaleString()}`
  59. time.style.fontSize = "12px"
  60. divMessage.appendChild(time)
  61. }
  62. }
  63. let nextMessageId = 0
  64. async function getMessages() {
  65. let data = await jsonPost("http://students.a-level.com.ua:10012", { func: 'getMessages', messageId: nextMessageId })
  66. showChat(data)
  67. nextMessageId = data.nextMessageId
  68. }
  69. const delay = ms => new Promise(ok => {
  70. setTimeout(() => ok(ms), ms)
  71. })
  72. async function checkLoop() {
  73. while (true) {
  74. getMessages()
  75. await delay(2000)
  76. }
  77. }
  78. checkLoop()
  79. </script>
  80. </body>
  81. </html>