Vika преди 3 години
родител
ревизия
ef2df94869
променени са 3 файла, в които са добавени 84 реда и са изтрити 0 реда
  1. 19 0
      js13/index.html
  2. 65 0
      js13/main.js
  3. 0 0
      js13/style.css

+ 19 - 0
js13/index.html

@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+  <link href="style.css" rel="stylesheet">
+</head>
+<body>
+  <div class='data-chat'>
+    <input type="text" id="nick" placeholder="nike">
+    <input type="text" id="message" placeholder="message">
+    <button id='sendMes'>Send</button>
+  </div>
+  <div id="chat"></div>
+  <script src="main.js"></script>
+</body>
+</html>

+ 65 - 0
js13/main.js

@@ -0,0 +1,65 @@
+let messageId = 0;
+
+async function sendMessage() {
+  let data = {
+    func: 'addMessage',
+    nick: `${nick.value}`, 
+    message: `${message.value}`,
+  };
+  try {
+    let response = await fetch('http://students.a-level.com.ua:10012', {
+      method: 'POST',
+      body: JSON.stringify(data),
+    });
+    
+    let result = await response.json(); 
+    messageId = result.nextMessageId; 
+  }
+  catch  (err) { 
+    console.log(err);
+  }
+    
+}
+
+function buildChat (obj) {
+  let arr = obj['data'];
+   
+  for (let i = 0; i < arr.length; i++) { 
+    const b = document.createElement('b');
+    const p = document.createElement('p');
+    chat.prepend(p);
+    p.append(b);
+    b.innerHTML = (`${arr[i]['nick']} :`);
+    p.append(`${arr[i]['message']}`);
+  }
+}
+
+async function getMessages() {
+  try {
+    let data = {
+      func: "getMessages", 
+      messageId: messageId,
+    };
+    let response = await fetch('http://students.a-level.com.ua:10012',{
+      method: 'POST',
+      body: JSON.stringify(data), 
+    });
+
+    let result = await response.json(); console.log(result);
+    buildChat(result);
+  }
+  catch (err) {
+    console.log(err);
+  }
+}
+
+async function sendAndCheck() {
+  Promise.all([sendMessage(), getMessages()])
+  nick.value = '';
+  message.value = '';
+}
+
+sendMes.onclick = sendAndCheck;
+
+
+setInterval(getMessages, 3000);

+ 0 - 0
js13/style.css