index.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="ru">
  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>Chat Async</title>
  8. </head>
  9. <style>
  10. *{
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family: fantasy;
  15. }
  16. .wrapper{
  17. display: flex;
  18. flex-direction: column;
  19. }
  20. input , #submitBtn{
  21. width: 100%;
  22. font-size: 2em;
  23. height: 44px;
  24. font-family: fantasy;
  25. margin: 2px;
  26. }
  27. </style>
  28. <body>
  29. <div class="wrapper">
  30. <input type="text" id="nameInput" placeholder="name" />
  31. <input type="text" id="messageInput" placeholder="message" />
  32. <button id="submitBtn">Submit</button>
  33. <div id="msgContent"></div>
  34. </div>
  35. <script>
  36. function jsonPost(url , data){
  37. return new Promise((resolve , reject) => {
  38. fetch(url , {
  39. method: "POST",
  40. body: JSON.stringify(data),
  41. }).then(
  42. (res) => {
  43. resolve(res.json());
  44. },
  45. (err) => {
  46. reject(new Error(err));
  47. }
  48. );
  49. });
  50. }
  51. let nextMessageId = 0;
  52. const viewMessage = (parent , elements) => {
  53. for(let message of elements){
  54. let messageContainer = document.createElement('div')
  55. let messageText = document.createElement('div')
  56. let messageNick = document.createElement('div')
  57. let date = document.createElement('div')
  58. date.innerHTML = new Date(message.timestamp).toLocaleTimeString()
  59. messageContainer.append(messageNick , messageText , date);
  60. messageNick.innerHTML = "nick :" + message.nick;
  61. messageContainer.style.fontSize = '25px'
  62. messageNick.style.color = 'red'
  63. messageText.style.marginLeft = '50px'
  64. date.style.textAlign = 'right'
  65. messageNick.style.fontWeight = 'bold'
  66. messageContainer.style.fontFamily = 'fantasy'
  67. messageText.innerHTML = message.message;
  68. messageContainer.classList.add('messageContainer')
  69. messageNick.classList.add('messageNick')
  70. messageText.classList.add('messageText')
  71. date.classList.add("date");
  72. parent.prepend(messageContainer)
  73. }
  74. };
  75. submitBtn.addEventListener("click" , function sendMessage(){
  76. let name = nameInput.value
  77. let text = messageInput.value
  78. if(name.length === null || messageInput.length === null){
  79. nameInput.classList.add('error')
  80. return;
  81. } else {
  82. nameInput.classList.remove('error')
  83. }
  84. sendAndCheck(name , text);
  85. })
  86. async function sendMessage(nick , message ){
  87. let nextMessageId = await jsonPost("http://students.a-level.com.ua:10012" , {
  88. func: "addMessage",
  89. nick: nick,
  90. message: message,
  91. });
  92. return nextMessageId;
  93. }
  94. async function getMessages(nextMessageId){
  95. let res = await jsonPost("http://students.a-level.com.ua:10012" , {
  96. func: "getMessages",
  97. messageId : nextMessageId,
  98. });
  99. if(nextMessageId < res.nextMessageId){
  100. viewMessage(msgContent , res.data);
  101. nextMessageId = res.nextMessageId;
  102. }
  103. return nextMessageId;
  104. }
  105. async function sendAndCheck(nick , message) {
  106. let nextMessageId = await sendMessage(nick , message);
  107. getMessages(nextMessageId)
  108. }
  109. let messageContent = setInterval(() => {
  110. jsonPost('http://students.a-level.com.ua:10012' , {
  111. func : "getMessages",
  112. messageId: nextMessageId ,
  113. }).then((res) => {
  114. if(nextMessageId !== res.nextMessageId){
  115. viewMessage(msgContent , res.data);
  116. nextMessageId = res.nextMessageId;
  117. }
  118. });
  119. },100);
  120. </script>
  121. </body>
  122. </html>