index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>await paradise (chat)</title>
  6. </head>
  7. <body>
  8. <div id="cont">
  9. </div>
  10. <script>
  11. //-----------stage 0
  12. let gbl=0;
  13. function jsonPost(url, data){
  14. return new Promise((resolve, reject) => {
  15. var x = new XMLHttpRequest();
  16. x.onerror = () => reject(new Error('jsonPost failed'))
  17. x.open("POST", url, true);
  18. x.send(JSON.stringify(data))
  19. x.onreadystatechange = () => {
  20. if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
  21. resolve(JSON.parse(x.responseText))
  22. }else if (x.status != 200){
  23. reject(new Error('status is not 200'))
  24. }
  25. }
  26. })
  27. }
  28. //-------------stage 1
  29. let n=document.createElement('input');
  30. let mes=document.createElement('input');
  31. let btn=document.createElement('input');
  32. btn.type='submit';
  33. btn.value='Send';
  34. n.placeholder='nick';
  35. mes.placeholder='message';
  36. let btn2=document.createElement('input');
  37. btn2.value='GetMessage';
  38. btn2.type='submit';
  39. cont.append(n,mes,btn,btn2);
  40. btn.onclick=()=>{
  41. jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: n.value, message: mes.value})
  42. .then(value =>{
  43. console.log(value);
  44. console.log(value.nextMessageId);
  45. },
  46. err=> console.log(err)
  47. );
  48. }
  49. btn2.onclick=()=>{
  50. jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: 0})
  51. .then(value => console.log(value), err=> console.log(err));
  52. }
  53. //----------------stage 2
  54. async function allMessages(){
  55. let div=document.createElement('div');
  56. let mail=await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages",messageId: 0});
  57. for(let item in mail.data){
  58. let p=document.createElement('p');
  59. let newdate = new Date(mail.data[item].timestamp).toLocaleString()
  60. p.innerHTML=`<b>${mail['data'][item]['nick']}</b>: ${mail['data'][item]['message']} ---- ${newdate}`;
  61. div.append(p);
  62. }
  63. document.body.append(div);
  64. gbl=mail.nextMessageId;
  65. }
  66. allMessages()
  67. //--------------stage 3
  68. async function getNewMessages(){
  69. let mail = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: gbl});
  70. let locale = mail.nextMessageId;
  71. gbl < locale ? gbl = locale : null;
  72. for (let item in mail.data) {
  73. let div = document.createElement('div');
  74. cont.append(div);
  75. let newdate = new Date(mail.data[item].timestamp).toLocaleString()
  76. div.innerHTML = `<b>${mail.data[item].nick}</b>: ${mail.data[item].message} <small><i>${newdate}</i></small>`;
  77. }
  78. return mail.data;
  79. }
  80. //getNewMessages();
  81. //------------------stage 4
  82. setInterval(()=>getNewMessages(),2000);
  83. //-------------------stage 5
  84. btn.addEventListener('click', buttonHandler);
  85. function buttonHandler() {
  86. sendAndCheck(n.value, mes.value) ;
  87. }
  88. async function sendMessage(nick,mes){
  89. jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: mes})
  90. .then(value =>{
  91. console.log(value);
  92. console.log(value.nextMessageId);
  93. },
  94. err=> console.log("something wrong: "+err)
  95. );
  96. }
  97. async function sendAndCheck(nick, message) {
  98. await Promise.all([sendMessage(nick, message), getNewMessages()]);
  99. }
  100. async function checkLoop() {
  101. let delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  102. while(true) {
  103. await delay(2000).then(() => getNewMessages())
  104. }
  105. }
  106. checkLoop()
  107. //----------------stage 6
  108. async function jsonPost2(url, data) {
  109. return fetch(url, {method: 'POST', body: JSON.stringify(data)})
  110. }
  111. jsonPost2("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: n.value, message: mes.value})
  112. .then(res=>res.json())
  113. .then(res=>console.log(res));
  114. </script>
  115. </body>
  116. </html>