-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
111 lines (97 loc) · 3.12 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
const socket = io('ws://localhost:3500')
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
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">${time}</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 += ","
}
})
}
}