-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
59 lines (52 loc) · 1.82 KB
/
client.js
File metadata and controls
59 lines (52 loc) · 1.82 KB
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
const socket=io("http://localhost:8000");
const form=document.getElementById('send');
const messageInp=document.getElementById('messageInp');
const messageConatiner=document.querySelector('.container');
var audio=new Audio('tone.mp3');
var conversation=new Audio('conversation.mp3');
// conversation
const append=(message,position)=>{
const messageElement=document.createElement('div');
messageElement.className=`flex-${position}`;
const innerMessage=document.createElement('div');
innerMessage.innerText=message;
innerMessage.classList.add('message');
innerMessage.classList.add(`${position}`);
messageElement.appendChild(innerMessage);
messageConatiner.appendChild(messageElement);
conversation.play();
console.log("sound played converation");
}
// audio
const appends=(message,position)=>{
const messageElement=document.createElement('div');
messageElement.className=`flex-${position}`;
const innerMessage=document.createElement('div');
const bold=document.createElement('b');
bold.innerText=message;
innerMessage.appendChild(bold);
innerMessage.classList.add('message');
innerMessage.classList.add(`${position}`);
messageElement.appendChild(innerMessage);
messageConatiner.appendChild(messageElement);
audio.play();
console.log("sound played audio");
}
form.addEventListener('submit',(e)=>{
e.preventDefault();
const message=messageInp.value;
append(`You: ${message}`,"right");
socket.emit('send',message);
messageInp.value="";
})
const naam=prompt("Enter your name");
socket.emit('new-user-joined',naam);
socket.on('user-joined',name=>{
appends(`${name} joined the chat`,"right")
})
socket.on('receive',data=>{
append(`${data.name}: ${data.message}`,'left');
})
socket.on('left',name=>{
appends(`${name} left the chat`,'left');
})