realtimechat_js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. let objPath = document.getElementById("path");
  2. let objPayload = document.getElementById("payload");
  3. let objCurrentServerStatus = document.getElementById("serverStatus");
  4. let objOnlineCounter = document.getElementById("online");
  5. let objOnlineList = document.getElementById("onlineList");
  6. let objMusicanotes = document.getElementById("musicanotes");
  7. let lastMessageId = document.getElementById("LMId").innerText;
  8. let reqIsFailed = false;
  9. let firstLoadingWithDisconnectedServer = false;
  10. let unreadedCount = 0;
  11. let soundEnabled = false;
  12. const ajaxUrl = document.getElementById("ajaxPath").innerText;
  13. const originalTitle = document.title;
  14. const audio = new Audio('/newmessage.mp3');
  15. const HTML_SERVER_ONLINE_MARKER = "✅";
  16. const HTML_SERVER_OFFLINE_MARKER = "❌";
  17. function sound() {
  18. if (!soundEnabled) {
  19. soundEnabled = true;
  20. objMusicanotes.style.opacity = "1";
  21. objMusicanotes.title = "Sound notifications enabled";
  22. } else {
  23. soundEnabled = false;
  24. objMusicanotes.style.opacity = ".3";
  25. objMusicanotes.title = "Sound notifications disabled";
  26. }
  27. }
  28. function appendMessage(nick /* if == "***", then system message */, message)
  29. {
  30. let messageObject = document.createElement("div");
  31. messageObject.setAttribute("class", "main_payload__chat");
  32. const date = new Date;
  33. messageObject.setAttribute("title", date.toLocaleString());
  34. let nicknameContainer = document.createElement("div");
  35. nicknameContainer.setAttribute("class", "main_payload__chat_username");
  36. if (nick === "***") {
  37. nicknameContainer.setAttribute("style", "color: white; text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;");
  38. nicknameContainer.innerText = "IRCaBot";
  39. } else {
  40. nicknameContainer.innerText = nick;
  41. nicknameContainer.setAttribute("style", "color: #1b4af5");
  42. if (document.hidden) {
  43. unreadedCount++;
  44. document.title = '(' + unreadedCount + ') ' + originalTitle;
  45. if (soundEnabled) {
  46. audio.play();
  47. }
  48. }
  49. }
  50. let textContainer = document.createElement("div");
  51. textContainer.setAttribute("class", "main_payload__chat_mail");
  52. textContainer.innerHTML = message;
  53. messageObject.appendChild(nicknameContainer);
  54. messageObject.appendChild(textContainer);
  55. objPayload.appendChild(messageObject);
  56. objPayload.scrollBy(0,1000);
  57. }
  58. function changeOnlineList(nicksArray)
  59. {
  60. objOnlineList.innerHTML = "";
  61. nicksArray.forEach(function(nick) {
  62. let nickObject = document.createElement("div");
  63. nickObject.setAttribute("class", "main_middle__online_point");
  64. nickObject.innerText = nick;
  65. objOnlineList.appendChild(nickObject);
  66. });
  67. }
  68. function toAJAX()
  69. {
  70. loop();
  71. let xhttp = new XMLHttpRequest();
  72. xhttp.onload = function() {
  73. if (reqIsFailed) {
  74. reqIsFailed = false;
  75. objPath.removeAttribute("style");
  76. }
  77. const answer = JSON.parse(xhttp.responseText);
  78. if (firstLoadingWithDisconnectedServer && answer.serverStatus) {
  79. firstLoadingWithDisconnectedServer = false;
  80. appendMessage("***", "<b>Connected to the server</b>. New messages will appear below!");
  81. objPath.removeAttribute("style");
  82. }
  83. if (!answer.status) {
  84. appendMessage("***", answer.message);
  85. return;
  86. }
  87. if (answer.serverStatusChanged) {
  88. if (answer.serverStatus) {
  89. objPath.removeAttribute("style");
  90. objCurrentServerStatus.innerText = HTML_SERVER_ONLINE_MARKER;
  91. } else {
  92. objPath.setAttribute("style", "color: red");
  93. objCurrentServerStatus.innerText = HTML_SERVER_OFFLINE_MARKER;
  94. }
  95. }
  96. if (answer.onlineUsersChanged) {
  97. const onlineInfo = answer.online;
  98. objOnlineCounter.innerText = onlineInfo.count;
  99. changeOnlineList(onlineInfo.list);
  100. }
  101. if (answer.LMIdChanged) {
  102. lastMessageId = answer.LMId;
  103. let msgArray = answer.newMessages;
  104. msgArray.forEach(function(singleMsg) {
  105. appendMessage(singleMsg.user, singleMsg.text);
  106. });
  107. }
  108. setTimeout(toAJAX, 2000); // 2 sec
  109. }
  110. xhttp.onerror = function() {
  111. if (!reqIsFailed) {
  112. reqIsFailed = true;
  113. objPath.setAttribute("style", "color: red");
  114. }
  115. setTimeout(toAJAX, 1000); // 1 sec
  116. }
  117. let currentServerStatus = objCurrentServerStatus.innerText === HTML_SERVER_ONLINE_MARKER;
  118. xhttp.open("GET", "/ajax/"+ajaxUrl+"?onlineCounter="+objOnlineCounter.innerText+"&messageId="+lastMessageId+"&serverStatus="+currentServerStatus);
  119. xhttp.send();
  120. }
  121. function loop()
  122. {
  123. if (!document.hidden && unreadedCount>0) {
  124. unreadedCount = 0;
  125. document.title = originalTitle;
  126. }
  127. let dots = objPath.innerText;
  128. if (dots === "...") {
  129. dots = ""
  130. } else {
  131. dots += ".";
  132. }
  133. objPath.innerText = dots;
  134. }
  135. function main()
  136. {
  137. sound();
  138. objPayload.scrollBy(0,100000);
  139. let objHr = document.getElementById("hr");
  140. if (objCurrentServerStatus.innerText === HTML_SERVER_OFFLINE_MARKER) {
  141. firstLoadingWithDisconnectedServer = true;
  142. objPath.setAttribute("style", "color: red");
  143. hr.innerHTML = "Now the logger is disconnected from the server, so there will be no new messages.";
  144. } else {
  145. hr.innerHTML = "<b>New messages will appear below<b>";
  146. }
  147. toAJAX();
  148. }
  149. main();