123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <!DOCTYPE html>
- <!-- saved from url=(0025)http://192.168.1.20:5000/ -->
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>18_practic_chat</title>
- </head>
- <body>
- <div id="formContainer"></div>
- <input type="text" id="nickId" />
- <input type="text" id="msgId" />
- <button id="sendId">Send msg</button>
- <div id="chatId">sdfasfasfvasafvasfabvfasdfbasbfas</div>
- <script>
- // jsonPost("http://students.a-level.com.ua:10012", {
- // func: "addMessage",
- // nick: "Sheva",
- // message: "++Hi++",
- // });
- 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"));
- }
- };
- });
- }
- sendId.onclick = function () {
- jsonPost("http://students.a-level.com.ua:10012", {
- func: "addMessage",
- nick: nickId.value,
- message: msgId.value,
- });
- };
- //
- //
- let startMsgId = 0;
- let date;
- setInterval(() => {
- let j = jsonPost("http://students.a-level.com.ua:10012", {
- func: "getMessages",
- messageId: startMsgId,
- }).then((x) => {
- startMsgId = x.nextMessageId;
- console.log(startMsgId);
- shouMsg(x.data);
- });
- let shouMsg = function (msgArr) {
- // console.log(msgArr);
- // msgArr.reverse();
- msgArr.forEach((element) => {
- date = new Date(element.timestamp);
- let str =
- date.getFullYear() +
- "/" +
- (date.getMonth() + 1) +
- "/" +
- date.getDate() +
- " " +
- date.getHours() +
- ":" +
- date.getMinutes() +
- ":" +
- date.getSeconds() +
- " " +
- element.nick +
- ": " +
- element.message +
- "<br>";
- chatId.insertAdjacentHTML("afterbegin", str);
- });
- };
- }, 5000);
- </script>
- </body>
- </html>
|