script.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. async function jsonPost(url, data)
  2. {
  3. console.log('here')
  4. // return new Promise((resolve, reject) => {
  5. // var x = new XMLHttpRequest();
  6. // x.onerror = () => reject(new Error('jsonPost failed'))
  7. // //x.setRequestHeader('Content-Type', 'application/json');
  8. // x.open("POST", url, true);
  9. // x.send(JSON.stringify(data))
  10. // x.onreadystatechange = () => {
  11. // if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
  12. // resolve(JSON.parse(x.responseText))
  13. // }
  14. // else if (x.status != 200){
  15. // reject(new Error('status is not 200'))
  16. // }
  17. // }
  18. // })
  19. const options = {
  20. method: "POST",
  21. headers: {
  22. "Accept": "application/json",
  23. "Content-Type": "application/json"
  24. },
  25. body: JSON.stringify(data)
  26. }
  27. try {
  28. console.log('here2')
  29. const response = await fetch(url, options)
  30. console.log('here3')
  31. const content = await response.json()
  32. return content
  33. } catch(e) {
  34. console.error('error ocurred', e)
  35. }
  36. }
  37. async function sendMessage(nick, message) {
  38. await jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: message})
  39. }
  40. async function drawHistory(messageId=0) {
  41. let messages = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: messageId})
  42. console.log(messages)
  43. msgTotal = messages.nextMessageId
  44. for(let message of messages.data) {
  45. let msgDiv = document.createElement('div')
  46. msgDiv.insertAdjacentHTML('beforeend', `
  47. <small style="color: magenta;">${new Date(message.timestamp).toLocaleString()}</small>
  48. <div>
  49. <h4 style="color: yellow; display: inline; font-style: italic;">${message.nick} :</h4> ${message.message}
  50. </div>
  51. `)
  52. msgDiv.style.width = 'fit-content'
  53. msgDiv.style.borderBottom = '1px solid grey'
  54. msgDiv.style.marginBottom = '25px'
  55. msgScreen.prepend(msgDiv)
  56. }
  57. console.log(msgTotal)
  58. let newStuff = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: msgTotal-1})
  59. console.log('new stufff', newStuff)
  60. }
  61. drawHistory()
  62. async function checkNewMessages() {
  63. let {nextMessageId} = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: msgTotal})
  64. if(nextMessageId > msgTotal) {
  65. console.log('new shit', msgTotal, nextMessageId)
  66. msgTotal = nextMessageId
  67. return true
  68. }
  69. console.log('no new shit', msgTotal, nextMessageId)
  70. return false
  71. }
  72. async function sendAndCheck() {
  73. sendMessage(nick, text)
  74. let oldMsgCount = msgTotal
  75. checkNewMessages()
  76. drawHistory(msgTotal - (msgTotal-oldMsgCount))
  77. }
  78. async function checkLoop() {
  79. while(true) {
  80. await delay(5000)
  81. let oldMsgCount = msgTotal
  82. if(checkNewMessages()) {
  83. drawHistory(msgTotal - (msgTotal-oldMsgCount))
  84. }
  85. }
  86. }
  87. checkLoop()
  88. function isFilled(field) {
  89. if(field !== '' && !/^[\s]*$/.test(field)) {
  90. return true
  91. }
  92. return false
  93. }
  94. async function delay(ms) {
  95. return new Promise(ok => setTimeout(() => ok(ms), ms))
  96. }
  97. let msgScreen = document.getElementById('msgField')
  98. let msgTotal = 0
  99. let nickInput = document.getElementById('nickbox')
  100. let msgInput = document.getElementById('msgbox')
  101. let sendBtn = document.getElementById('sendBtn')
  102. sendBtn.disabled = true
  103. let nick = ''
  104. let text = ''
  105. nickInput.oninput = () => {
  106. nick = nickInput.value;
  107. isFilled(nick) && isFilled(text)? sendBtn.disabled = false : sendBtn.disabled = true
  108. console.log(nick);
  109. }
  110. msgInput.oninput = () => {
  111. text = msgInput.value;
  112. isFilled(nick) && isFilled(text)? sendBtn.disabled = false : sendBtn.disabled = true
  113. console.log(text, isFilled(text), isFilled(nick));
  114. }
  115. sendBtn.onclick = () => sendAndCheck()
  116. // setInterval(()=>{
  117. // checkNewMessages()
  118. // }, 5000)