-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
45 lines (34 loc) · 1.12 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
40
41
42
43
44
45
const socket = io('http://localhost:8000')
const form = document.getElementById('send-container')
const messageinput = document.getElementById('messageinp')
const messagecontainer = document.querySelector('.container')
var audio = new Audio('ting.mp3');
const append = (message, position) => {
const messagevent = document.createElement('div')
messagevent.innerText = message;
messagevent.classList.add('message')
messagevent.classList.add(position)
messagecontainer.append(messagevent)
if (position == 'right') {
audio.play();
}
}
const Name = prompt("enter your name to join")
socket.emit('new-user-joined', Name)
socket.on('user-joined', name => {
append(`${name} joined the chat`, 'right')
})
socket.on('receive', data => {
append(`${data.name} :${data.message}`, 'left')
console.log(data)
})
socket.on('left', name => {
append(`${name} joined the chat`, 'right')
})
form.addEventListener('submit', (e) => {
e.preventDefault();
const message = messageinput.value;
append(`you:${message}`, 'right')
socket.emit('send', message)
messageinput.value = ' ';
})