Browse Source

homework13 done

holevchuk.evgeny 1 year ago
parent
commit
06b3c61b82
12 changed files with 333 additions and 0 deletions
  1. 13 0
      hw13/stage1.html
  2. 33 0
      hw13/stage1.js
  3. 11 0
      hw13/stage2.html
  4. 35 0
      hw13/stage2.js
  5. 14 0
      hw13/stage3.html
  6. 43 0
      hw13/stage3.js
  7. 14 0
      hw13/stage4.html
  8. 50 0
      hw13/stage4.js
  9. 14 0
      hw13/stage5.html
  10. 62 0
      hw13/stage5.js
  11. 13 0
      hw13/stage6.html
  12. 31 0
      hw13/stage6.js

+ 13 - 0
hw13/stage1.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<input id="nickInput" type="text"><br>
+<input id="messageInput" type="text"><br>
+<button id="sendButton" type="button">Отправить</button>
+<script src="stage1.js"></script>
+</body>
+</html>

+ 33 - 0
hw13/stage1.js

@@ -0,0 +1,33 @@
+function jsonPost(url, data) {
+	return new Promise((resolve, reject) => {
+		const 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'))
+			}
+		}
+	});
+}
+
+const url = "http://students.a-level.com.ua:10012";
+
+sendButton.addEventListener('click', () => {
+	let nickValue = nickInput.value;
+	let messageValue = messageInput.value;
+
+	const newPost = jsonPost(url, {
+			func: 'addMessage',
+			nick: nickValue,
+			message: messageValue
+		}
+	);
+
+	newPost.then(data => console.log(data.nextMessageId))
+});

+ 11 - 0
hw13/stage2.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<div id="chatMessages"></div>
+<script src="stage2.js"></script>
+</body>
+</html>

+ 35 - 0
hw13/stage2.js

@@ -0,0 +1,35 @@
+function jsonPost(url, data) {
+	return new Promise((resolve, reject) => {
+		const 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'))
+			}
+		}
+	});
+}
+
+const url = "http://students.a-level.com.ua:10012";
+
+let messagesArray = jsonPost(url, {func: 'getMessages'});
+
+const addZero = data => data < 10 ? `0${data}` : data;
+
+const buildDate = date => `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${addZero(date.getHours())}:${addZero(date.getMinutes())}:${addZero(date.getSeconds())}`;
+
+const buildMessage = (nick, message, time) => {
+  return `<p>nick - ${nick}, message - ${message}, date - ${buildDate(new Date(time))}</p>`;
+}
+
+messagesArray.then(messages => {
+	for (const message of messages.data) {
+		chatMessages.insertAdjacentHTML('afterbegin', buildMessage(message.nick, message.message, message.timestamp));
+	}
+});

+ 14 - 0
hw13/stage3.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<input id="nickInput" type="text"><br>
+<input id="messageInput" type="text"><br>
+<button id="sendButton" type="button">Отправить</button>
+<div id="chatMessages"></div>
+<script src="stage3.js"></script>
+</body>
+</html>

+ 43 - 0
hw13/stage3.js

@@ -0,0 +1,43 @@
+function jsonPost(url, data) {
+	return new Promise((resolve, reject) => {
+		const 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'))
+			}
+		}
+	});
+}
+
+const url = "http://students.a-level.com.ua:10012";
+let nextId = 0;
+
+sendButton.addEventListener('click', () => {
+	let nickValue = nickInput.value;
+	let messageValue = messageInput.value;
+
+	const addZero = data => data < 10 ? `0${data}` : data;
+
+	const buildDate = date => `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${addZero(date.getHours())}:${addZero(date.getMinutes())}:${addZero(date.getSeconds())}`;
+
+	const buildMessage = (nick, message, time) => {
+		return `<p>nick - ${nick}, message - ${message}, date - ${buildDate(new Date(time))}</p>`;
+	}
+
+	jsonPost(url, {func: 'addMessage', nick: nickValue, message: messageValue});
+
+	let messagesArray = jsonPost(url, {func: 'getMessages', messageId: nextId});
+	messagesArray.then(messages => {
+		for (const message of messages.data) {
+			chatMessages.insertAdjacentHTML('afterbegin', buildMessage(message.nick, message.message, message.timestamp));
+		}
+		nextId = messages.nextMessageId;
+	});
+});

+ 14 - 0
hw13/stage4.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<input id="nickInput" type="text"><br>
+<input id="messageInput" type="text"><br>
+<button id="sendButton" type="button">Отправить</button>
+<div id="chatMessages"></div>
+<script src="stage4.js"></script>
+</body>
+</html>

+ 50 - 0
hw13/stage4.js

@@ -0,0 +1,50 @@
+function jsonPost(url, data) {
+	return new Promise((resolve, reject) => {
+		const 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'))
+			}
+		}
+	});
+}
+
+//Stage 0 && Stage 1
+const url = "http://students.a-level.com.ua:10012";
+let nextId = 0;
+
+const addZero = data => data < 10 ? `0${data}` : data;
+
+const buildDate = date => `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${addZero(date.getHours())}:${addZero(date.getMinutes())}:${addZero(date.getSeconds())}`;
+
+const buildMessage = (nick, message, time) => {
+	return `<p>nick - ${nick}, message - ${message}, date - ${buildDate(new Date(time))}</p>`;
+}
+
+const getMessages = () => {
+	let messagesArray = jsonPost(url, {func: 'getMessages', messageId: nextId});
+	messagesArray.then(messages => {
+		for (const message of messages.data) {
+			chatMessages.insertAdjacentHTML('afterbegin', buildMessage(message.nick, message.message, message.timestamp));
+		}
+		nextId = messages.nextMessageId;
+	});
+}
+
+const sendMessage = (nick, message) => jsonPost(url, {func: 'addMessage', nick: nick, message: message});
+
+sendButton.addEventListener('click', () => {
+	let nickValue = nickInput.value;
+	let messageValue = messageInput.value;
+	sendMessage(nickValue, messageValue);
+	getMessages();
+});
+
+setInterval(getMessages, 5000);

+ 14 - 0
hw13/stage5.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<input id="nickInput" type="text"><br>
+<input id="messageInput" type="text"><br>
+<button id="sendButton" type="button">Отправить</button>
+<div id="chatMessages"></div>
+<script src="stage5.js"></script>
+</body>
+</html>

+ 62 - 0
hw13/stage5.js

@@ -0,0 +1,62 @@
+function jsonPost(url, data) {
+	return new Promise((resolve, reject) => {
+		const 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'))
+			}
+		}
+	});
+}
+
+//Stage 0 && Stage 1
+const url = "http://students.a-level.com.ua:10012";
+let nextId = 0;
+
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
+
+const addZero = data => data < 10 ? `0${data}` : data;
+
+const buildDate = date => `${date.getDate()}/${date.getMonth()+1}/${date.getFullYear()} ${addZero(date.getHours())}:${addZero(date.getMinutes())}:${addZero(date.getSeconds())}`;
+
+const buildMessage = (nick, message, time) => {
+	return `<p>nick - ${nick}, message - ${message}, date - ${buildDate(new Date(time))}</p>`;
+}
+
+const sendMessage = async (nick, message) => {
+	await jsonPost(url, {func: 'addMessage', nick: nick, message: message});
+}
+
+const getMessages = async () => {
+	let messagesArray = await jsonPost(url, {func: 'getMessages', messageId: nextId});
+	for (const message of messagesArray.data) {
+		chatMessages.insertAdjacentHTML('afterbegin', buildMessage(message.nick, message.message, message.timestamp));
+	}
+	nextId = messagesArray.nextMessageId;
+}
+
+const sendAndCheck = async (nick, message) => {
+	await sendMessage(nick, message);
+	getMessages();
+}
+
+const checkLoop = async () => {
+	await delay(5000);
+	getMessages();
+	checkLoop();
+}
+
+sendButton.addEventListener('click', () => {
+	let nickValue = nickInput.value;
+	let messageValue = messageInput.value;
+	sendAndCheck(nickValue, messageValue);
+});
+
+checkLoop();

+ 13 - 0
hw13/stage6.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Title</title>
+</head>
+<body>
+<input id="nickInput" type="text"><br>
+<input id="messageInput" type="text"><br>
+<button id="sendButton" type="button">Отправить</button>
+<script src="stage6.js"></script>
+</body>
+</html>

+ 31 - 0
hw13/stage6.js

@@ -0,0 +1,31 @@
+const jsonPost = async (url, data) => {
+	try {
+		const response = await fetch(url, {
+			method: 'POST',
+			body: JSON.stringify(data)
+		});
+		if (!response.ok) {
+			new Error(`An error has occurred: ${response.status}`);
+		} else {
+			return JSON.parse(await response.text());
+		}
+	} catch (e) {
+		return e;
+	}
+}
+
+const url = "http://students.a-level.com.ua:10012";
+
+sendButton.addEventListener('click', async () => {
+	let nickValue = nickInput.value;
+	let messageValue = messageInput.value;
+
+	const newPost = await jsonPost(url, {
+			func: 'addMessage',
+			nick: nickValue,
+			message: messageValue
+		}
+	);
+
+	console.log(newPost.nextMessageId);
+});