using both unload methods for disconnecting

This commit is contained in:
Baipyrus 2023-01-18 15:19:22 +01:00
parent a098da6f09
commit 88568d8274
3 changed files with 31 additions and 29 deletions

View File

@ -22,6 +22,7 @@
<a id="current"></a>
<input type="text" id="input">
</div>
<script type="text/javascript" src="js/networking.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

View File

@ -1,32 +1,3 @@
// Handle server connection
window.addEventListener('beforeunload', () => {
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";
let pretext = {
original: "root@baipyr.us:~# ",
current: "root@baipyr.us:~# "

30
js/networking.js Normal file
View File

@ -0,0 +1,30 @@
// 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";