-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
55 lines (45 loc) · 1.25 KB
/
script.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
let socket = io()
$(function () {
let containerLogin = $('#login')
let containerChat = $('#chatbox')
containerChat.hide()
let btnLogin = $('#btn-login')
let btnSend = $('#btn-send')
let inpUsername = $('#inp-username')
let inpMsg = $('#inp-msg')
let listChats = $('#chatlist')
btnLogin.click(() => {
socket.emit('login', {
username: inpUsername.val()
})
})
socket.on('logged_in', (data) => {
if (data.success) {
containerChat.show()
containerLogin.hide()
}
})
btnSend.click(() => {
socket.emit('chat', {
message: inpMsg.val()
})
})
socket.on('chat', (data) => {
let cardExtraClass = (data.private)
? 'text-white bg-info'
: ''
listChats.append(
$(
`
<div class="card ${cardExtraClass} col-12">
<div class="card-body">
<div class="card-title">${data.sender}</div>
<div class="card-subtitle text-muted small">${data.timestamp}</div>
<div class="card-text">${data.message}</div>
</div>
</div>
`
)
)
})
})