-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
36 lines (32 loc) · 1.26 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
import io from 'socket.io-client'
(() => {
let socket = io();
let form = document.getElementsByClassName("main__form")[0];
let message = document.getElementsByClassName("main__message")[0];
let messages = document.getElementsByClassName("main__messages")[0];
let nick;
let nicknameButton = document.getElementsByClassName("main__nicknameButton")[0];
nicknameButton.onclick=()=>{
let nicknameDialog=document.getElementsByClassName("main__blackout")[0];
let nicknameInput=document.getElementsByClassName("main__nicknameInput")[0];
nick=nicknameInput.value;
nicknameDialog.style.display="none";
};
form.onsubmit = () => {
socket.emit('chat message', nick, message.value);
message.value = "";
return false;
};
socket.on('chat message', (nickname, message) => {
let div = document.createElement("div");
let divWrapper= document.createElement("div");
divWrapper.classList.add("message");
div.classList.add("messageContent");
if (nickname === nick){
div.classList.add("myMessage");
}
div.textContent = `${nickname}: ${message}`;
divWrapper.appendChild(div);
messages.appendChild(divWrapper);
});
})();