12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- function jsonPost(url, data) {
- return new Promise((resolve, reject) => {
- var x = new XMLHttpRequest();
- x.onerror = () => reject(new Error('jsonPost failed'))
- x.open("POST", url, true);
- x.send(JSON.stringify(data))
- x.onreadystatechange = () => {
- if (x.readyState == XMLHttpRequest.DONE && x.status == 200){
- resolve(JSON.parse(x.responseText))
- }
- else if (x.status != 200){
- reject(new Error('status is not 200'))
- }
- }
- })
- }
- let inp1 = document.querySelector('.user-name'),
- inp2 = document.querySelector('.user-massege'),
- butn = document.querySelector('.send-massege'),
- chat = document.querySelector('.chat');
- function sayComrad(){
- function checkerson(){
- inp1.value == '' || inp2.value == ''? butn.classList.add('block-btn'): butn.classList.remove('block-btn');
- }
-
- inp1.oninput=inp2.oninput=checkerson;
-
- butn.onclick = () => {
- (inp1.value == '' || inp2.value == '') ? reject() : resolve();
- function resolve(){
- jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: inp1.value, message: inp2.value});
- inp1.value = '';
- inp2.value = '';
- }
- function reject() {
- butn.classList.add('block-btn');
- }
- }
- }
- sayComrad()
- let smsNamb = 0
- function getChatData() {
- jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: smsNamb})
- .then((d) => createSpaceForChat(d));
- }
- getChatData()
- function createSpaceForChat(data) {
- console.log(smsNamb)
- for (let details of data.data){
- if (details.nick === '' || details.massage === '' || typeof(details.nick) === 'object' || typeof(details.nick) === 'undefined') {
- continue;
- };
- let main = document.createElement('div');
- let iner = document.createElement('div');
- main.appendChild(iner);
-
- let timeSend = document.createElement('time');
- let ttiimmee = new Date(details.timestamp);
- timeSend.innerText = ((ttiimmee.getHours()+'').length>1 ? ttiimmee.getHours() : '0'+ttiimmee.getHours()) +':'+ ((ttiimmee.getMinutes()+'').length>1 ? ttiimmee.getMinutes() : '0'+ttiimmee.getMinutes());
- main.appendChild(timeSend);
-
- let userName = document.createElement('b');
- userName.innerText = details.nick + ':';
- iner.appendChild(userName);
-
- let message = document.createElement('time');
- message.innerText = details.message;
- iner.appendChild(message);
-
- chat.prepend(main)
- }
- smsNumb = data.nextMessageId;
- update();
- }
- function update(){
- setInterval(getChatData(),100)
- }
|