-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.02.js
201 lines (147 loc) · 5.02 KB
/
main1.02.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function searchUser(name) {
fetchSearch(name);
}
function searchUsername(name) {
fetchUsername(name);
}
function verifyUsername(b) {
var unameWarning = document.getElementById("usernameWarning");
var signupButton = document.getElementById("signupBtn");
if(b == 1) {
unameWarning.removeAttribute("hidden");
signupButton.disabled = true;
} else {
unameWarning.setAttribute("hidden", true);
signupButton.disabled = false;
}
}
function fetchUsername(name) {
fetch('https://otanga.co.ke/Projects/Chat-App-PHP/unameverify.php', {
method: 'POST',
body: new URLSearchParams('name=' + name)
})
.then(res => res.json())
.then(res => verifyUsername(res))
.catch(e => console.error('Error: ' + e))
}
function fetchSearch(name) {
fetch('https://otanga.co.ke/Projects/Chat-App-PHP/usearch.php', {
method: 'POST',
body: new URLSearchParams('name=' + name)
})
.then(res => res.json())
.then(res => showUserList(res))
.catch(e => console.error('Error: ' + e))
}
function showUserList(data) {
const uList = document.getElementById("userList");
uList.innerHTML = "";
for(let i = 0; i < data.length; i++) {
const div = document.createElement("div");
const messageDiv = document.createElement("div");
const span = document.createElement("span");
const a = document.createElement("a");
const b = document.createElement("b");
const br = document.createElement("br");
div.classList.add("card", "py-3", "m-1");
a.classList.add("btn", "mainbg", "white");
span.style.color = "black";
messageDiv.style.width = "100%";
messageDiv.style.textAlign = "center";
b.innerHTML = data[i];
span.appendChild(b);
a.innerHTML = "Message";
a.href = "chat/?to=" + data[i];
div.appendChild(span);
div.appendChild(br);
messageDiv.appendChild(a);
div.appendChild(messageDiv);
uList.appendChild(div);
}
}
function sendText() {
const message = document.getElementById('msg').value;
const receiver = document.getElementById('rec').value;
const sender = document.getElementById('sen').value
postMessage(sender, receiver, message);
}
function postMessage(sender, receiver, message) {
var bodyParams = new URLSearchParams('message=' + message);
bodyParams.append("sender", sender);
bodyParams.append("receiver", receiver);
fetch('messageapi.php', {
method: 'POST',
body: bodyParams
})
.then(res => updateMessage(message))
.then(res => livereceiving(sender, receiver))
.catch(e => console.error('Error: ' + e))
}
function updateMessage(message) {
const textbox = document.getElementById("msg");
textbox.value = "";
const chatbox = document.getElementById("chatbox");
const span = document.createElement("span");
const br = document.createElement("br");
span.innerHTML = message;
span.classList.add("rounded");
span.classList.add("p-1");
span.style.display = "block";
span.style.width = "fit-content";
span.style.marginLeft = "auto";
span.style.marginRight = "0";
span.style.backgroundColor = "#dcf8c6";
chatbox.appendChild(span);
chatbox.appendChild(br);
}
function receiveMessage(message, id) {
var latestChat = document.getElementById("lcid");
if(message.length < 1) {
return;
}
//if latest chat is already received skip this function
if(latestChat.textContent == id) {
return;
}
const chatbox = document.getElementById("chatbox");
const span = document.createElement("span");
const br = document.createElement("br");
span.innerHTML = message;
span.classList.add("rounded");
span.classList.add("p-1");
span.style.display = "block";
span.style.width = "fit-content";
span.style.marginLeft = "0";
span.style.marginRight = "auto";
span.style.backgroundColor = "#fff5c4";
chatbox.appendChild(span);
chatbox.appendChild(br);
latestChat.textContent = id;
}
async function livereceiver(user, receiver) {
const delay = ms => new Promise(res => setTimeout(res, ms));
await delay(1000);
var latestChat = document.getElementById("lcid");
var lcid = latestChat.textContent;
var bodyParams = new URLSearchParams('user=' + user);
bodyParams.append("receiver", receiver);
bodyParams.append("lcid", lcid);
let response = await fetch("livereceiverapi.php", {
method: 'POST',
body: bodyParams
})
if(response.status == 500) {
await livereceiver(user, receiver);
} else if(response.status != 200) {
await new Promise(resolve => setTimeout(resolve, 1000));
await livereceiver(user, receiver);
} else {
var data = response.json();
data
.then(res => receiveMessage(res[0], res[1]))
await livereceiver(user, receiver);
}
}
function livereceiving(user, receiver) {
livereceiver(user, receiver);
}