-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.js
42 lines (33 loc) · 1.03 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
const socket=('http://localhost:8000');
const form=document.getElementById('Send-container');
const messageInput=document.getElementById('messageInp');
const messageContainer=document.querySelector(".container")
const append=(message,postion)=>{
const messageElement=document.createElement('div');
messageElement.innerText=message;
messageElement.classList.add('message');
messageElement.classList.add(postion);
messageContainer.append(messageElement);
}
form.addEventListener('submit',(e)=>
{
e.preventDefult();
const message=messageInput.value;
append(`you:${message}`,'right');
socket.emit('send',message);
messageInput.value='';
})
const name=prompt("Enter Your name to join");
socket.emit('new-user-joined',name);
socket.normalize('user-joined',name=>
{
append(`${name} joined the chat`,'right');
})
socket.on('receive',data=>
{
append(`${data.name}:${data.message}`,'left')
})
socket.on('left',name=>
{
append(`${name} left the chat`,'left')
})