TerminalHomepage/js/networking.js

240 lines
6.9 KiB
JavaScript
Raw Normal View History

// Global networking variables
2023-02-02 08:16:40 +00:00
const pretext = {
2023-02-02 07:12:12 +00:00
original: "root@baipyr.us:~# ",
current: "root@baipyr.us:~# "
};
const userData = { history: [], chat: null };
2023-02-02 08:16:40 +00:00
let chatMode = "default", activityNotify;
2023-02-08 14:30:06 +00:00
// If no user data exists, initialize it
2023-02-02 07:12:12 +00:00
if (window.localStorage.getItem("id") === null)
window.localStorage.setItem("id", "");
if (window.localStorage.getItem("name") === null)
window.localStorage.setItem("name", "");
if (window.localStorage.getItem("connected") === null)
window.localStorage.setItem("connected", "");
2023-02-02 08:16:40 +00:00
2023-02-08 14:30:06 +00:00
// Activity request interval
2023-02-02 08:16:40 +00:00
function setNotification() {
return setInterval(()=>{
fetch('/activity', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id")
})
}).then(res => {});
}, 60000);
}
2023-02-08 14:30:06 +00:00
// If user data is found, check for validity
2023-02-02 08:16:40 +00:00
if (window.localStorage.getItem("id") !== "" && window.localStorage.getItem("name") !== "") {
2023-02-02 07:12:12 +00:00
pretext.original = pretext.current = `${window.localStorage.getItem("name")}@baipyr.us:~# `;
2023-02-02 08:16:40 +00:00
fetch('/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id")
})
}).then(res => {
2023-02-08 14:30:06 +00:00
// Return JSON if status is OK
2023-02-02 08:16:40 +00:00
if (res.status === 200)
return res.json();
}).then(res => {
if (res === undefined)
return;
2023-02-08 14:30:06 +00:00
// Regularly send activity
2023-02-02 08:16:40 +00:00
if (res.success)
activityNotify = setNotification();
2023-02-08 14:30:06 +00:00
// Delete user data
2023-02-02 08:16:40 +00:00
else {
window.localStorage.setItem("id", "");
window.localStorage.setItem("name", "");
window.localStorage.setItem("connected", "");
pretext.original = pretext.current = "root@baipyr.us:~# ";
cmd_clear();
}
});
}
// Tell server to disconnect
2023-02-02 07:12:12 +00:00
function disconnect() {
if (!!window.localStorage.getItem("connected")) {
2023-02-08 14:30:06 +00:00
// Send disconnect request
fetch('/disconnect', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
2023-02-02 07:12:12 +00:00
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id")
})
}).then(res => {});
2023-02-08 14:30:06 +00:00
// Delete user data
2023-02-02 07:12:12 +00:00
window.localStorage.setItem("id", "");
window.localStorage.setItem("name", "");
window.localStorage.setItem("connected", "");
2023-02-08 14:30:06 +00:00
// Reset pretext
2023-02-02 07:12:12 +00:00
pretext.original = pretext.current = "root@baipyr.us:~# ";
}
}
// Connect to server
function connect(name) {
2023-02-02 07:12:12 +00:00
window.localStorage.setItem("name", name);
fetch('/connect', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ name })
}).then(res => {
if (res.status === 200) {
2023-02-08 14:30:06 +00:00
// Regularly send activity
2023-02-02 08:16:40 +00:00
activityNotify = setNotification();
2023-02-02 07:12:12 +00:00
window.localStorage.setItem("connected", "1");
return res.json();
}
// No valid status code was sent
console.error("Could not connect to server!");
}).then(res => {
if (res === undefined)
return;
2023-02-08 14:30:06 +00:00
// Save provided id, set pretext, output confirmation
const { id } = res;
2023-02-02 07:12:12 +00:00
window.localStorage.setItem("id", id);
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
outputText({output: `Connected as '${name}#${id}'.`});
});
}
// Send nickname to server, receive verification
function sendNickname(name) {
2023-02-02 07:12:12 +00:00
const oldName = window.localStorage.getItem("name");
window.localStorage.setItem("name", name);
fetch('/nickname', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
2023-02-02 07:12:12 +00:00
id: window.localStorage.getItem("id"),
oldName,
name
})
}).then(res => {
2023-02-08 14:30:06 +00:00
// Return JSON if status is OK
if (res.status === 200)
return res.json();
}).then(res => {
// No valid status code
if (res === undefined)
return;
// Receive name and save it
const { id } = res;
2023-02-02 07:12:12 +00:00
window.localStorage.setItem("id", id);
pretext.original = pretext.current = `${name}@baipyr.us:~# `;
// Reply whether name is taken or not
outputText({output: `Applied name '${name}#${id}'.`});
});
}
// Send a direct message, do not look at response
2023-02-02 09:45:07 +00:00
function directMessage(names, message) {
fetch('/message', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
2023-02-02 07:12:12 +00:00
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id"),
2023-02-02 09:45:07 +00:00
to: names,
message
})
}).then(res => {});
}
2023-02-03 12:57:29 +00:00
// Send a group message
function groupMessage(chat, message) {
fetch('/sendGroup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id"),
message,
chat
})
}).then(res => {});
}
// Request all usernames and number of anonymous users
function requestUsernames() {
fetch('/getNames', {
method: 'GET',
headers: {
'Accept': 'application/json',
}
}).then(res => {
2023-02-08 14:30:06 +00:00
// Return JSON if status is OK
if (res.status === 200)
return res.json();
}).then(res => {
if (res === undefined)
return;
2023-02-08 14:30:06 +00:00
// Output usernames
let output = "";
for (const u of res)
output += `${u.name}#${u.id} `;
outputText({ output });
});
2023-01-30 07:17:32 +00:00
}
// Ping host for two-way-delay
function requestPing() {
const startTime = new Date();
2023-02-02 08:16:40 +00:00
fetch('/ping', {
method: 'GET'
2023-02-02 08:16:40 +00:00
}).then(res => {
2023-01-30 07:17:32 +00:00
if (res.status !== 200)
return;
2023-02-08 14:30:06 +00:00
// Output measured ping time
2023-01-30 07:17:32 +00:00
const diff = new Date() - startTime;
const output = `Host responded after ${diff}ms.`;
outputText({output});
2023-01-30 07:17:32 +00:00
});
2023-02-03 12:57:29 +00:00
}
// Initialize group chat
function sendChatInit(chat) {
fetch('/chatInit', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id"),
chat
})
}).then(res => {});
}