script.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. async function jsonPost(url, data)
  2. {
  3. const options = {
  4. method: "POST",
  5. body: JSON.stringify(data)
  6. }
  7. try {
  8. const response = await fetch(url, options)
  9. const content = await response.json()
  10. return content
  11. } catch(e) {
  12. console.error('error ocurred', e)
  13. }
  14. }
  15. async function sendMessage(nick, message) {
  16. await jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: message})
  17. }
  18. async function drawHistory(messageId=0) {
  19. let messages = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: messageId})
  20. msgTotal = messages.nextMessageId
  21. for(let message of messages.data) {
  22. let msgDiv = document.createElement('div')
  23. msgDiv.insertAdjacentHTML('beforeend', `
  24. <small style="color: gold;">${new Date(message.timestamp).toLocaleString()}</small>
  25. <div style="word-wrap: break-word; color: skyblue;">
  26. <h4 style="color: mediumseagreen; font-style: italic; display: inline;">${message.nick} :</h4> ${message.message}
  27. </div>
  28. `)
  29. msgDiv.style.width = '100%'
  30. msgDiv.style.borderBottom = '1px solid grey'
  31. msgDiv.style.marginBottom = '25px'
  32. msgScreen.prepend(msgDiv)
  33. }
  34. let newStuff = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: msgTotal-1})
  35. }
  36. drawHistory()
  37. async function checkNewMessages() {
  38. let {nextMessageId} = await jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: msgTotal})
  39. if(nextMessageId > msgTotal) {
  40. msgTotal = nextMessageId
  41. return true
  42. }
  43. return false
  44. }
  45. async function sendAndCheck() {
  46. sendMessage(nick, text)
  47. let oldMsgCount = msgTotal
  48. checkNewMessages()
  49. drawHistory(msgTotal - (msgTotal-oldMsgCount))
  50. }
  51. async function checkLoop() {
  52. while(true) {
  53. await delay(5000)
  54. let oldMsgCount = msgTotal
  55. if(checkNewMessages()) {
  56. drawHistory(msgTotal - (msgTotal-oldMsgCount))
  57. }
  58. }
  59. }
  60. checkLoop()
  61. function isFilled(field) {
  62. if(field !== '' && !/^[\s]*$/.test(field)) {
  63. return true
  64. }
  65. return false
  66. }
  67. async function delay(ms) {
  68. return new Promise(ok => setTimeout(() => ok(ms), ms))
  69. }
  70. let msgScreen = document.getElementById('msgField')
  71. let msgTotal = 0
  72. let nickInput = document.getElementById('nickbox')
  73. let msgInput = document.getElementById('msgbox')
  74. let sendBtn = document.getElementById('sendBtn')
  75. sendBtn.disabled = true
  76. let nick = ''
  77. let text = ''
  78. nickInput.oninput = () => {
  79. nick = nickInput.value;
  80. isFilled(nick) && isFilled(text)? sendBtn.disabled = false : sendBtn.disabled = true
  81. }
  82. msgInput.oninput = () => {
  83. text = msgInput.value;
  84. isFilled(nick) && isFilled(text)? sendBtn.disabled = false : sendBtn.disabled = true
  85. }
  86. sendBtn.onclick = () => sendAndCheck()