TerminalHomepage/js/networking.js

233 lines
6.8 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: [], chatPull: null };
2023-02-02 08:16:40 +00:00
let chatMode = "default", activityNotify;
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
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);
}
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 => {
if (res.status === 200)
return res.json();
}).then(res => {
if (res === undefined)
return;
if (res.success)
activityNotify = setNotification();
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")) {
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-02 07:12:12 +00:00
window.localStorage.setItem("id", "");
window.localStorage.setItem("name", "");
window.localStorage.setItem("connected", "");
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-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;
// Save provided id
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 => {
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}'.`});
});
}
// Pull incoming chat messages
function getChatMessages() {
fetch('/getChat', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
2023-02-02 09:45:07 +00:00
from: chatMode.split(' ')[1].split(','),
2023-02-02 07:12:12 +00:00
name: window.localStorage.getItem("name"),
id: window.localStorage.getItem("id")
})
}).then(res => {
// Incoming messages
if (res.status === 200)
return res.json();
// No new messages
else if (res.status === 204)
return;
cmd_exit("Error occurred while fetching messages!");
}).then(res => {
// No messages or invalid status code
if (res === undefined)
return;
// Output all messages by user
for (const user in res)
for (const msg of res[user])
outputText({
preNext: `${user}: `,
output: msg
});
});
}
// 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 => {});
}
// function globalMessage(message) {}
// Request all usernames and number of anonymous users
function requestUsernames() {
fetch('/getNames', {
method: 'GET',
headers: {
'Accept': 'application/json',
}
}).then(res => {
if (res.status === 200)
return res.json();
}).then(res => {
if (res === undefined)
return;
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',
headers: {
'Accept': 'application/json',
}
}).then(res => {
2023-01-30 07:17:32 +00:00
if (res.status !== 200)
return;
const diff = new Date() - startTime;
const output = `Host responded after ${diff}ms.`;
outputText({output})
});
}