-
Notifications
You must be signed in to change notification settings - Fork 0
/
clint.js
40 lines (34 loc) · 1.21 KB
/
clint.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
// const io = require("socket.io-client");//
const socket = io('http://localhost:8000');
const form = document.getElementById('send-container');
const messageInput = document.getElementById('messageInp')
const messageContainer = document.querySelector('.container')
var audio = new Audio('source/ring.mp3')
const append = (message, position) => {
const messageElement = document.createElement('div');
messageElement.innerText = message;
messageElement.classList.add('message');
messageElement.classList.add(position);
messageContainer.append(messageElement);
if (position == 'left') {
audio.play();
}
}
form.addEventListener('submit', (e)=>{
e.preventDefault();
const message = messageInput.value;
append(`you: ${message}`, 'right');
socket.emit('send', message);
messageInput.value = ''
})
const names = prompt("Enter your name to join");
socket.emit('new-user-joined', names);
socket.on('user-joined', names =>{
append(`${names}: joined the chat`, 'right')
})
socket.on('receive', data =>{
append(`${data.names}: ${data.message}`, 'left')
})
socket.on('left', names =>{
append(`${names }: left the chat`,'left')
})