diff options
| -rw-r--r-- | backend/index.ts | 10 | ||||
| -rw-r--r-- | src/index.ts | 34 | ||||
| -rw-r--r-- | src/modules/helpers.ts | 2 | ||||
| -rw-r--r-- | src/modules/messages.ts | 10 | ||||
| -rw-r--r-- | tsconfig.json | 2 |
5 files changed, 25 insertions, 33 deletions
diff --git a/backend/index.ts b/backend/index.ts index 851b84e..b57cb4d 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -1,18 +1,15 @@ import express from "express"; -// import cors from "cors"; const app = express(); const PORT = 8001; app.use(express.json()); -// app.use(cors()); app.use("/", express.static("../dist")); const longpoll = require("express-longpoll")(app); -longpoll.create("/poll"/* , cors() */); +longpoll.create("/poll"); app.post("/send", (req, res) => { - // console.log(req.body); longpoll.publish("/poll", req.body) res.send("OK"); }); @@ -20,8 +17,3 @@ app.post("/send", (req, res) => { app.listen(PORT, () => { console.log(`[server]: Server is running at https://localhost:${PORT}`); }); - -// const data: Message = { text: "Some data", color: "#654321", username: "maks" }; -// setInterval(function () { -// longpoll.publish("/poll", data); -// }, 5000);
\ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 670e861..5845a7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,23 +35,21 @@ export default class Irc { this.poll(); } - poll(): void { + async poll(): Promise<void> { console.log("polling"); - $.ajax({ - url: this.messages.url + "poll", - success: msg => { - this.messages.addMessage(msg); - const update = - document.activeElement === this.input ? "bottom" : "relative"; - this.helpers.updateScrollbar(update); - this.poll(); - }, - error: () => { - this.poll(); - }, - timeout: 30000, // 30 seconds - }); + fetch(this.messages.url + "poll") + .then(msg => + msg.json().then(msg => { + this.messages.addMessage(msg); + const update = + document.activeElement === this.input ? "bottom" : "relative"; + + this.helpers.updateScrollbar(update); + this.poll(); + }) + ) + .catch(this.poll); } messageExe(msg: string): boolean { @@ -69,7 +67,7 @@ export default class Irc { if (msg.startsWith("quit") === true) { msgToSend.text = `User ${this.username} has left the chat`; this.messages.sendMessage(msgToSend); - + this.helpers.suicide( "Thank you for using this site, you can now safely close it." ); @@ -79,7 +77,7 @@ export default class Irc { if (this.helpers.isColor(color) === true) { msgToSend.text = `User ${this.username} has changed his color to ${color}`; this.messages.sendMessage(msgToSend); - + this.color = color; } else { this.divError.innerText = "You need to give correct color!"; @@ -92,7 +90,7 @@ export default class Irc { } else { msgToSend.text = `User ${this.username} has changed his nick to ${username}`; this.messages.sendMessage(msgToSend); - + this.username = username; } } else { diff --git a/src/modules/helpers.ts b/src/modules/helpers.ts index 862f121..ab5ed15 100644 --- a/src/modules/helpers.ts +++ b/src/modules/helpers.ts @@ -2,7 +2,7 @@ export default class Helpers { //@ts-expect-error readonly chatListScroll = $("#chatlist").tinyscrollbar(); - isColor(strColor: string) { + isColor(strColor: string): boolean { const s = new Option().style; s.color = strColor; return s.color !== ""; diff --git a/src/modules/messages.ts b/src/modules/messages.ts index 45e9446..67e3983 100644 --- a/src/modules/messages.ts +++ b/src/modules/messages.ts @@ -16,7 +16,6 @@ export default class Messages { async addMessage(msg: Message): Promise<void> { const board = $(".overview")[0], - time = new Date(msg.time), div = document.createElement("div"), spans = [ document.createElement("span"), @@ -24,11 +23,14 @@ export default class Messages { document.createElement("span"), ]; - let min = new Date().getMinutes().toString(); + let min = new Date().getMinutes().toString(), + h = new Date().getHours().toString(); + min = min.length === 1 ? "0" + min : min; + h = h.length === 1 ? "0" + h : h; - spans[0].innerText = `[${time.getHours()}:${min}] `; - spans[1].innerText = `<@${msg.username}> `; + spans[0].innerText = `[${h}:${min}] `; + spans[1].innerText = msg.username === "Server" ? `<${msg.username}> ` : `<@${msg.username}> `; spans[1].style.color = msg.color; spans[2].innerText = msg.text; diff --git a/tsconfig.json b/tsconfig.json index 80e3ba9..898b6e8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "declaration": true, /* Generates corresponding '.d.ts' file. */ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - "sourceMap": true, /* Generates corresponding '.map' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ // "outDir": "./", /* Redirect output structure to the directory. */ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ |
