main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* function jsonPost(url, data)
  2. {
  3. return new Promise((resolve, reject) => {
  4. var x = new XMLHttpRequest();
  5. x.onerror = () => reject(new Error('jsonPost failed'))
  6. //x.setRequestHeader('Content-Type', 'application/json');
  7. x.open("POST", url, true);
  8. x.send(JSON.stringify(data))
  9. x.onreadystatechange = () => {
  10. if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
  11. resolve(JSON.parse(x.responseText))
  12. }
  13. else if (x.status != 200){
  14. reject(new Error('status is not 200'))
  15. }
  16. }
  17. })
  18. } */
  19. let sendButton = document.getElementById('send');
  20. let nick = document.getElementById('nick');
  21. let message = document.getElementById('msg');
  22. let chat = document.getElementById('chat');
  23. sendButton.addEventListener('click', () =>{
  24. let nickValue = nick.value;
  25. let messageValue = message.value;
  26. sendMessage(nickValue,messageValue);
  27. message.value = '';
  28. })
  29. /* jsonPost("http://students.a-level.com.ua:10012")
  30. .then((result) => {
  31. /* console.log(result.data);
  32. let chat = document.getElementById('chat');
  33. for(let message of result.data){
  34. chat.innerHTML = `${message.nick} : ${message.message}`;
  35. } */
  36. /* getMessage();
  37. console.log(nextMessageId);
  38. }) */
  39. /* .then((data)=> getMessage(data)) */
  40. async function sendAndCheck(nick,message){
  41. await sendMessage(nick,message);
  42. await getMessage();
  43. }
  44. async function getMessage(messageId = 0){
  45. /* jsonPost2("http://students.a-level.com.ua:10012",{func: "getMessages", messageId:messageId})
  46. .then((result) => {
  47. let chat = document.getElementById('chat');
  48. let reversedData = result.data.reverse();
  49. reversedData.forEach(element => {
  50. let divMsg = document.createElement('div');
  51. let nickSpan = document.createElement('span');
  52. let messageSpan = document.createElement('span');
  53. nickSpan.innerHTML += element.nick;
  54. messageSpan.innerHTML += `: ${element.message}`;
  55. divMsg.appendChild(nickSpan,);
  56. divMsg.appendChild(messageSpan);
  57. chat.append(divMsg);
  58. });
  59. }) */
  60. let res = await jsonPost2("http://students.a-level.com.ua:10012",{func: "getMessages", messageId:messageId})
  61. /* let chat = document.getElementById('chat'); */
  62. chat.innerHTML = '';
  63. let reversedData = res.data.reverse();
  64. reversedData.forEach((element) => {
  65. /* let divMsg = document.createElement('div');
  66. let nickSpan = document.createElement('span');
  67. let messageSpan = document.createElement('span');
  68. nickSpan.innerHTML += element.nick;
  69. messageSpan.innerHTML += `: ${element.message}`;
  70. divMsg.appendChild(nickSpan,);
  71. divMsg.appendChild(messageSpan);
  72. chat.append(divMsg); */
  73. drawMessages(element);
  74. });
  75. /* let prevId = res.nextMessageId;
  76. let currentId = messageId;
  77. if(currentId<prevId){
  78. let result = await jsonPost2("http://students.a-level.com.ua:10012",{func: "getMessages", messageId:currentId+1});
  79. let reversed = result.data.reverse();
  80. reversed.forEach((element) => {
  81. drawMessages(element);
  82. })
  83. } */
  84. }
  85. function drawMessages(element){
  86. let divMsg = document.createElement('div');
  87. let nickSpan = document.createElement('span');
  88. let messageSpan = document.createElement('span');
  89. nickSpan.innerHTML += element.nick;
  90. messageSpan.innerHTML += `: ${element.message}`;
  91. divMsg.appendChild(nickSpan,);
  92. divMsg.appendChild(messageSpan);
  93. chat.append(divMsg);
  94. }
  95. async function sendMessage(userNick,userMessage) {
  96. /* jsonPost2("http://students.a-level.com.ua:10012",{func: "addMessage",nick:userNick,message: userMessage})
  97. .then((result) => {
  98. console.log(result);
  99. getMessage(result.nextMessageId);
  100. }); */
  101. let res = await jsonPost2("http://students.a-level.com.ua:10012",{func: "addMessage",nick:userNick,message: userMessage})
  102. getMessage(res.nextMessageId);
  103. message.value = '';
  104. }
  105. const delay = (ms) => new Promise((ok) => setTimeout(() => ok(ms), ms));
  106. async function checkLoop(){
  107. await getMessage()
  108. .then(() => delay(2000))
  109. .then(checkLoop);
  110. }
  111. getMessage();
  112. async function jsonPost2(url,data){
  113. let result = await fetch(url,{
  114. method: 'POST',
  115. body: JSON.stringify(data)
  116. });
  117. if(result.status == 200){
  118. let json = await result.json();
  119. return json
  120. } else{
  121. throw new Error(result.statusText);
  122. }
  123. }
  124. checkLoop();
  125. /* getMessage(); */
  126. /* jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: "Anon", message: 'Я не умею копипастить в консоль, зато умею жать красную кнопку.'}) */