-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
32 lines (28 loc) · 969 Bytes
/
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
const socket =io('http://localhost:3000')
const messageContainer = document.getElementById('message-container')
const messageForm = document.getElementById('send-container')
const messageInput = document.getElementById('message-input')
const name=prompt('What is your name?')
appendMessage('You joined')
socket.emit('new-user',name)
socket.on('chat-message',data =>{
appendMessage(data.name+':'+data.message)
})
socket.on('user-connected',name =>{
appendMessage(name+' connected')
})
socket.on('user-disconnected',name =>{
appendMessage(name+' disconnected')
})
messageForm.addEventListener('submit', e=>{
e.preventDefault()
const message = messageInput.value
appendMessage('You :'+message)
socket.emit('send-chat-message',message)
messageInput.value=''
})
function appendMessage(message){
const messageElement=document.createElement('div')
messageElement.innerText=message
messageContainer.append(messageElement)
}