-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (106 loc) Β· 3.31 KB
/
app.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const socket = io("https://basic-chat-back-end-production.up.railway.app/");
// const socket = io("ws://localhost:8000");
const msgInput = document.querySelector("#message");
const nameInput = document.querySelector("#name");
const chatRoom = document.querySelector("#room");
const activity = document.querySelector(".activity");
const usersList = document.querySelector(".user-list");
const roomList = document.querySelector(".room-list");
const chatDisplay = document.querySelector(".chat-display");
function sendMessage(e) {
e.preventDefault();
if (nameInput.value && msgInput.value && chatRoom.value) {
socket.emit("message", {
name: nameInput.value,
text: msgInput.value,
});
msgInput.value = "";
}
msgInput.focus();
}
function enterRoom(e) {
e.preventDefault();
if (nameInput.value && chatRoom.value) {
socket.emit("enterRoom", {
name: nameInput.value,
room: chatRoom.value,
});
}
}
document.querySelector(".form-msg").addEventListener("submit", sendMessage);
document.querySelector(".form-join").addEventListener("submit", enterRoom);
msgInput.addEventListener("keypress", () => {
socket.emit("activity", nameInput.value);
});
// Listen for messages
socket.on("message", (data) => {
activity.textContent = "";
const { name, text, time } = data;
// Format date to local time
let options = {
// year: "numeric",
// month: "long",
// day: "numeric",
hour: "2-digit",
minute: "2-digit",
// second: "2-digit",
};
const formattedTimeString = new Date(time).toLocaleDateString("en-US", options);
const li = document.createElement("li");
li.className = "post";
if (name === nameInput.value) li.className = "post post--left";
if (name !== nameInput.value && name !== "Admin")
li.className = "post post--right";
if (name !== "Admin") {
li.innerHTML = `<div class="post__header ${
name === nameInput.value ? "post__header--user" : "post__header--reply"
}">
<span class="post__header--name">${name}</span>
<span class="post__header--time">${formattedTimeString}</span>
</div>
<div class="post__text">${text}</div>`;
} else {
li.innerHTML = `<div class="post__text">${text}</div>`;
}
document.querySelector(".chat-display").appendChild(li);
chatDisplay.scrollTop = chatDisplay.scrollHeight;
});
let activityTimer;
socket.on("activity", (name) => {
activity.textContent = `${name} is typing...`;
// Clear after 3 seconds
clearTimeout(activityTimer);
activityTimer = setTimeout(() => {
activity.textContent = "";
}, 3000);
});
socket.on("userList", ({ users }) => {
showUsers(users);
});
socket.on("roomList", ({ rooms }) => {
showRooms(rooms);
});
function showUsers(users) {
usersList.textContent = "";
if (users) {
usersList.innerHTML = `<em>Users in ${chatRoom.value}:</em>`;
users.forEach((user, i) => {
usersList.textContent += ` ${user.name}`;
if (users.length > 1 && i !== users.length - 1) {
usersList.textContent += ",";
}
});
}
}
function showRooms(rooms) {
roomList.textContent = "";
if (rooms) {
roomList.innerHTML = "<em>Active Rooms:</em>";
rooms.forEach((room, i) => {
roomList.textContent += ` ${room}`;
if (rooms.length > 1 && i !== rooms.length - 1) {
roomList.textContent += ",";
}
});
}
}