using both unload methods for disconnecting
This commit is contained in:
parent
a098da6f09
commit
88568d8274
@ -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>
|
29
js/main.js
29
js/main.js
@ -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
30
js/networking.js
Normal 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";
|
Loading…
Reference in New Issue
Block a user