TerminalHomepage/js/networking.js

30 lines
811 B
JavaScript
Raw Normal View History

// Handle server connection
document.body.onunload = document.body.onbeforeunload = () => {
if (user.connected)
fetch('/disconnect', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: user.id
})
}).then(res => {});
};
fetch('/connect').then(res => {
if (res.status === 200) {
user.connected = true;
return res.json();
}
console.error("Could not connect to server!");
}).then(res => {
if (res === undefined)
return;
const { id } = res;
user.id = id;
});
const user = { id: "", name: "", history: [], connected: false, chatPull: null };
let chatMode = "default";