-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
33 lines (28 loc) · 1023 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
33
const socket = io("https://anish853.github.io/web-_chat/");
const messageContainer = document.getElementById('message-container');
const messageForm = document.getElementById('send-container');
const messageInput = document.getElementById('message-input');
const nam = prompt('What is your name?');
appendMessage('You joined');
socket.emit('new-user', nam);
socket.on('chat-message', data => {
appendMessage(`${data.nam}: ${data.message}`)
})
socket.on('user-connected', nam => {
appendMessage(`${nam} connected`)
})
socket.on('user-disconnected', nam => {
appendMessage(`${nam} 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)
}