-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
129 lines (111 loc) · 3.95 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const socket = io('https://codachat-server.herokuapp.com/');
const form = document.querySelector("#input-form");
const messageInput = document.querySelector("#message-input");
const messageContainer = document.querySelector(".message-section");
const onlineUsers = document.querySelector(".online-users");
var audio = new Audio("./media/notification.mp3");
const appendMessage = (name,message,time,position) =>{
const messageElement = document.createElement("div");
messageElement.classList.add("message");
messageElement.classList.add(position);
const nameBody = document.createElement("h6");
nameBody.innerText = name;
nameBody.classList.add("user-name");
const messageBody = document.createElement("p");
messageBody.innerText = message;
messageBody.classList.add("user-message");
const timeBody = document.createElement("span");
timeBody.innerText = time;
timeBody.classList.add("time");
messageElement.append(nameBody);
messageElement.append(messageBody);
messageElement.append(timeBody);
messageContainer.append(messageElement);
if(position=="left"){
audio.play()
.catch(()=>{
console.log("Can't play");
})
}
}
const userJoined = (name,gender,userId)=>{
const userContainer = document.createElement("div");
userContainer.classList.add("user");
userContainer.setAttribute("id",userId);
const userDetails = document.createElement("div");
userDetails.classList.add("user-details");
const userName = document.createElement("h3");
userName.innerText = name;
userName.setAttribute("id","name");
const userStatus = document.createElement("h6");
userStatus.innerText = "online";
userStatus.setAttribute("id","status");
const userImg = document.createElement("div");
userImg.classList.add("user-img");
if(gender=="M" || gender=="m"){
userImg.classList.add("male");
}else{
userImg.classList.add("female");
}
userDetails.append(userName);
userDetails.append(userStatus);
userContainer.append(userDetails);
userContainer.append(userImg);
onlineUsers.append(userContainer);
}
const userLeft = (userId)=>{
const leftUser = document.querySelector(`#${userId}`);
leftUser.firstChild.lastChild.textContent = "offline";
setTimeout(()=>{
leftUser.remove();
},10000);
}
const checkUser = (userId)=>{
if(document.getElementById(`${userId}`)){
return true;
}else{
return false;
}
}
form.addEventListener("submit",(e)=>{
e.preventDefault();
const message = messageInput.value;
var current = new Date();
var currentTime = current.toLocaleTimeString();
appendMessage("You",message,currentTime,'right');
socket.emit("send",{message,gender})
messageInput.value = "";
})
function fn1() {
const nameInput= document.getElementById("username").value;
const gender= document.getElementById("gender").value;
if(nameInput != "" && gender != "")
{
var a= document.querySelector(".container1");
a.style.display="none";
var b= document.querySelector(".container2");
b.style.display="block";
userJoined(nameInput,gender,"self");
socket.emit("new-user-joined",{nameInput,gender});
socket.on("user-joined", (data)=>{
var current = new Date();
var currentTime = current.toLocaleTimeString();
userJoined(data.nameInput,data.gender,data.userId);
appendMessage(data.nameInput,"Joined the live chat",currentTime,"left");
})
}
}
socket.on("receive",data=>{
var current = new Date();
var currentTime = current.toLocaleTimeString();
appendMessage(data.name,data.message,currentTime,"left")
if(!checkUser(data.userId)){
userJoined(data.name,data.gender,data.userId)
}
})
socket.on("left",(data)=>{
var current = new Date();
var currentTime = current.toLocaleTimeString();
appendMessage(data.name,"Left the live chat :(",currentTime,"left")
userLeft(data.userId);
})