-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrontend.js
52 lines (43 loc) · 1.35 KB
/
Frontend.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
46
47
48
49
50
51
52
const socket = io('http://localhost:3000')
const messageContainer = document.getElementById('message-container')
const messageForm = document.getElementById('send-container')
const messageInput = document.getElementById('message-input')
appendMessage('You joined')
let naam;
while (!naam) {
naam = prompt('What is your name?');
if (naam === null) {
// alert('Name is required to join the chat.');
naam = prompt('What is your name?');
} else {
naam = naam.trim();
if (naam.length === 0) {
alert('Name cannot be empty.');
naam = null;
}
}
}
socket.emit('new user',naam)
socket.on('chat-message', data =>{
appendMessage(`${data.naam}: ${data.message}`)
})
socket.on('user-connected', naam =>{
appendMessage(`${naam} connected`)
})
socket.on('user-disconnected', naam =>{
appendMessage(`${naam} disconnected`)
})
messageForm.addEventListener('submit', e =>{
e.preventDefault()
const message = messageInput.value
if(message.length>0 && message.trim().length != 0){
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)
}