|
| 1 | +document.addEventListener("DOMContentLoaded", init); |
| 2 | + |
| 3 | +function init() { |
| 4 | + // dom references |
| 5 | + const messageBox = document.querySelector("#message"); |
| 6 | + const sendBtn = document.querySelector(".send"); |
| 7 | + const regBtn = document.querySelector(".regBtn"); |
| 8 | + const username = document.querySelector("#username"); |
| 9 | + const registerBox = document.querySelector(".register-box"); |
| 10 | + const section2 = document.querySelector(".section-2"); |
| 11 | + const userList = document.querySelector(".users-list"); |
| 12 | + const messages = document.querySelector(".allMessages"); |
| 13 | + const typingUsers = document.querySelector("#typingUsers"); |
| 14 | + |
| 15 | + let isTyping = false; |
| 16 | + |
| 17 | + // animations |
| 18 | + const animations = { |
| 19 | + fadeOut: [{ opacity: 1 }, { opacity: 0 }], |
| 20 | + fadeIn: [{ opacity: 0 }, { opacity: 1 }], |
| 21 | + }; |
| 22 | + |
| 23 | + // some constant values |
| 24 | + const url = "ws://localhost:3004"; |
| 25 | + |
| 26 | + // adding some functionalities to the WebSocket instance |
| 27 | + WebSocket.prototype.emit = function (event, data) { |
| 28 | + this.send(JSON.stringify({ event, data })); |
| 29 | + }; |
| 30 | + |
| 31 | + WebSocket.prototype.listen = function (eventName, callback) { |
| 32 | + this._socketEvents = this._socketEvents || {}; |
| 33 | + this._socketEvents[eventName] = this._socketEvents[eventName] || []; |
| 34 | + this._socketEvents[eventName].push(callback); |
| 35 | + }; |
| 36 | + |
| 37 | + const websocket = new WebSocket(url); |
| 38 | + |
| 39 | + function handleRegister(ev) { |
| 40 | + const msg = username.value; |
| 41 | + if (!msg) { |
| 42 | + alert("enter some username"); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + websocket.emit("user", { msg }); |
| 47 | + |
| 48 | + registerBox.animate(animations.fadeOut, { |
| 49 | + duration: 300, |
| 50 | + easing: "linear", |
| 51 | + }).onfinish = (ev) => { |
| 52 | + ev.target.effect.target.remove(); |
| 53 | + }; |
| 54 | + |
| 55 | + section2.animate(animations.fadeIn, { |
| 56 | + duration: 600, |
| 57 | + easing: "linear", |
| 58 | + fill: "forwards", |
| 59 | + delay: 300, |
| 60 | + }); |
| 61 | + |
| 62 | + username.value = ""; |
| 63 | + } |
| 64 | + |
| 65 | + function handleKeyPress(ev) { |
| 66 | + const message = ev.currentTarget.value; |
| 67 | + if (message) { |
| 68 | + sendBtn.disabled = false; |
| 69 | + if (!isTyping) { |
| 70 | + websocket.emit("addTyping"); |
| 71 | + isTyping = true; |
| 72 | + } |
| 73 | + } else { |
| 74 | + sendBtn.disabled = true; |
| 75 | + websocket.emit("removeTyping"); |
| 76 | + isTyping = false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function handleVisibilityChange() { |
| 81 | + if (document.visibilityState === "visible") { |
| 82 | + websocket.emit("updateUserStatus", { msg: "green" }); |
| 83 | + } else { |
| 84 | + websocket.emit("updateUserStatus", { msg: "yellow" }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + function sendMessage() { |
| 89 | + const msg = messageBox.value; |
| 90 | + if (msg) { |
| 91 | + websocket.emit("oldMessage", { msg }); |
| 92 | + websocket.emit("removeTyping"); |
| 93 | + isTyping = false; |
| 94 | + messageBox.value = ""; |
| 95 | + sendBtn.disabled = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + function onOpen() { |
| 100 | + console.log("web socket connection is open"); |
| 101 | + messageBox.addEventListener("keyup", handleKeyPress); |
| 102 | + sendBtn.addEventListener("click", sendMessage); |
| 103 | + regBtn.addEventListener("click", handleRegister); |
| 104 | + document.addEventListener("visibilitychange", handleVisibilityChange); |
| 105 | + |
| 106 | + // adding some events and their listeners |
| 107 | + |
| 108 | + websocket.listen("newMessage", (messageArray) => { |
| 109 | + // clear the old html |
| 110 | + messages.innerHTML = ""; |
| 111 | + const fragment = document.createDocumentFragment(); |
| 112 | + |
| 113 | + messageArray.forEach((message) => { |
| 114 | + const p = document.createElement("p"); |
| 115 | + p.textContent = `${message.username}: ${message.msg}`; |
| 116 | + fragment.appendChild(p); |
| 117 | + }); |
| 118 | + |
| 119 | + messages.appendChild(fragment); |
| 120 | + }); |
| 121 | + |
| 122 | + websocket.listen("allUsers", (users) => { |
| 123 | + // clear the old html |
| 124 | + userList.innerHTML = ""; |
| 125 | + |
| 126 | + const fragment = document.createDocumentFragment(); |
| 127 | + users.forEach((user) => { |
| 128 | + if (user) { |
| 129 | + const li = document.createElement("li"); |
| 130 | + li.textContent = `${user.username}`; |
| 131 | + li.classList.add(user.status); |
| 132 | + fragment.appendChild(li); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + userList.appendChild(fragment); |
| 137 | + }); |
| 138 | + |
| 139 | + websocket.listen("typingUsers", (users) => { |
| 140 | + console.log(users.length); |
| 141 | + let str = ""; |
| 142 | + if (!users.length) { |
| 143 | + // clear the old content |
| 144 | + typingUsers.textContent = ""; |
| 145 | + return; |
| 146 | + }; |
| 147 | + |
| 148 | + if (users.length === 1) { |
| 149 | + str = `${users[0]} is typing...`; |
| 150 | + } else { |
| 151 | + str = "".concat( |
| 152 | + users[0], |
| 153 | + " and ", |
| 154 | + users.length - 1, |
| 155 | + " more are typing..." |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + typingUsers.textContent = str; |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + function onMessage(ev) { |
| 164 | + try { |
| 165 | + const { event, data } = JSON.parse(ev.data); |
| 166 | + console.log(event, data); |
| 167 | + websocket._socketEvents[event].forEach((callback) => { |
| 168 | + callback(data); |
| 169 | + }); |
| 170 | + } catch (err) { |
| 171 | + // ignore errors due to some string text or binary data from server |
| 172 | + // those data not needed for this app |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + function onClose() { |
| 177 | + console.log("closed"); |
| 178 | + messageBox.removeEventListener("keyup", handleKeyPress); |
| 179 | + sendBtn.removeEventListener("click", sendMessage); |
| 180 | + regBtn.removeEventListener("click", handleRegister); |
| 181 | + document.removeEventListener("visibilitychange", handleVisibilityChange); |
| 182 | + } |
| 183 | + |
| 184 | + function onError(err) { |
| 185 | + console.log(err); |
| 186 | + } |
| 187 | + |
| 188 | + websocket.addEventListener("open", onOpen); |
| 189 | + websocket.addEventListener("error", onError); |
| 190 | + websocket.addEventListener("close", onClose); |
| 191 | + websocket.addEventListener("message", onMessage); |
| 192 | +} |
0 commit comments