-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
39 lines (38 loc) · 1.39 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function initializeChatBox() {
const localStorageValue = localStorage.getItem("messages");
console.log(localStorageValue);
const messages = (localStorageValue ? localStorageValue.split("#") : []).map(
(item) => {
if (item) return item;
}
);
messages?.forEach((item) => {
const paragraphElement = document.createElement("p");
paragraphElement.innerText = item;
const chatBox = document.querySelector(".chatBox");
chatBox.appendChild(paragraphElement);
});
}
initializeChatBox();
const socket = io("http://localhost:3000/");
socket.on("connect", (data) => {
const sendBtn = document.getElementById("sendBtn");
sendBtn.addEventListener("click", (e) => {
const textBoxt = document.getElementById("text");
const message = textBoxt.value;
if (!message) return alert("textbox cannot be empty");
socket.emit("clientMessage", message);
textBoxt.value = "";
});
});
socket.on("serverMessage", (message) => {
let localStorageValue = localStorage.getItem("messages")
? localStorage.getItem("messages") + "#" + message
: message;
console.log(localStorageValue);
localStorage.setItem("messages", localStorageValue);
const paragraphElement = document.createElement("p");
paragraphElement.innerText = message;
const chatBox = document.querySelector(".chatBox");
chatBox.appendChild(paragraphElement);
});