async print to console

This commit is contained in:
Baipyrus 2023-01-18 08:02:42 +01:00
parent 73914a1bdd
commit 2515618d9e

View File

@ -18,6 +18,7 @@ app.get('/connect', (req, res) => {
users.push({ users.push({
socket: req.socket, socket: req.socket,
date: new Date(), date: new Date(),
chats: [],
name: "", name: "",
id id
}); });
@ -40,6 +41,15 @@ app.post('/disconnect', (req, res) => {
app.post('/nickname', (req, res) => { app.post('/nickname', (req, res) => {
const { id, name } = req.body; const { id, name } = req.body;
let success = true;
for (const u of users)
if (u.name === name) {
success = false;
break;
}
if (success) {
for (const u of users) for (const u of users)
if (u.id === id) { if (u.id === id) {
if (logging) if (logging)
@ -47,7 +57,21 @@ app.post('/nickname', (req, res) => {
u.name = name; u.name = name;
break; break;
} }
res.status(200).send(); }
res.status(200).json({ success });
});
app.post('/chatInit', (req, res) => {
const { id, name } = req.body;
for (const u of users)
if (u.id === id) {
u.chats.push({
messages: [],
name
});
break;
}
}); });
app.post('/message', (req, res) => { app.post('/message', (req, res) => {
@ -58,15 +82,17 @@ app.post('/message', (req, res) => {
name = u.name; name = u.name;
break; break;
} }
let user = `User '${name}'`; let user = `User '${name}'`;
if (!name) if (!name)
user = name = "Anonymous" user = name = "Anonymous"
if (logging) { if (logging) {
const idStr = (!name) ? ` ID: ${id}.` : ''; const idStr = (!name) ? ` ID: ${id}.` : '';
console.log(`${user} sent message '${message}'.${idStr}`); console.log(`${user} sent message '${message}'.${idStr}`);
} }
res.status(200).json({ message });
res.status(200).send();
}); });
server.on('error', err => { server.on('error', err => {