Browse Source

HW14 4 stages done

Vadym Hlushko 3 years ago
parent
commit
9f8ce78360
3 changed files with 89 additions and 0 deletions
  1. 3 0
      hw14/.vscode/settings.json
  2. 17 0
      hw14/index.html
  3. 69 0
      hw14/main.js

+ 3 - 0
hw14/.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "liveServer.settings.port": 5501
+}

+ 17 - 0
hw14/index.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <input type="text" name="" id="nickInput" placeholder="Enter your nickname"> <br>
+    <input type="text" name="" id="msgInput" placeholder="Enter your message"> <br>
+    <button type="submit" id="sendButton"></button>
+    <div id="chat">
+
+    </div>
+    <script src="main.js"></script>
+</body>
+</html>

+ 69 - 0
hw14/main.js

@@ -0,0 +1,69 @@
+function jsonPost(url, data)
+{
+    return new Promise((resolve, reject) => {
+        var x = new XMLHttpRequest();   
+        x.onerror = () => reject(new Error('jsonPost failed'))
+        //x.setRequestHeader('Content-Type', 'application/json');
+        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 sendBtn = document.getElementById('sendButton')
+sendBtn.onclick = function sendMsg () {
+    let nick = document.getElementById('nickInput').value
+    let msg = document.getElementById('msgInput').value
+    jsonPost("http://students.a-level.com.ua:10012", {func: 'addMessage', nick: nick, message: msg })
+}
+sendBtn.innerHTML = "Send"
+let nickInput = document.getElementById('nickInput')
+let msgInput = document.getElementById('msgInput')
+nickInput.style.width = '99.5%'
+msgInput.style.width = '99.5%'
+sendBtn.style.width = '100%'
+nickInput.style.height = '40px'
+msgInput.style.height = '40px'
+msgInput.style.marginTop = '20px'
+sendBtn.style.marginTop = '20px'
+sendBtn.style.height = '40px'
+nickInput.style.fontSize = '20px'
+msgInput.style.fontSize = '20px'
+sendBtn.style.fontSize = '20px'
+
+function addMsg () {
+    let startMsg = 0;
+    setInterval(function(){
+        jsonPost("http://students.a-level.com.ua:10012", {func: "getMessages", messageId: startMsg}).then (result => {console.log(result.data)
+        for(startMsg in result.data) {
+            let chat = document.getElementById('chat')
+            let divMessage = document.createElement('div')
+            divMessage.style.border = '1px solid black'
+            divMessage.style.borderRadius = '10px'
+            divMessage.style.marginTop = '10px'
+            divMessage.style.backgroundColor = '#1E90FF'
+            divMessage.style.color = 'white'
+            let pMsg = document.createElement('p')
+            pMsg.innerHTML = result.data[startMsg].nick + ':' + ' ' + result.data[startMsg].message
+            pMsg.style.marginLeft = '20px'
+            pMsg.style.fontSize = '30px'
+            let time = document.createElement('p')
+            time.innerHTML = `${new Date(result.data[startMsg].timestamp).toLocaleString()}`
+            time.style.marginLeft = '20px'
+            divMessage.append(pMsg)
+            divMessage.append(time)
+            chat.prepend(divMessage)
+        }
+        startMsg = result.nextMessageId})
+    },2000)
+}
+addMsg()
+