init
This commit is contained in:
27
web-static/web.js
Normal file
27
web-static/web.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// window.addEventListener('load', function () {
|
||||
console.log("Page loaded... connecting to server...");
|
||||
const webSocket = new WebSocket(`ws://${window.location.host}`);
|
||||
var last_contact = Date.now();
|
||||
setInterval( () => {
|
||||
if (last_contact + 5000 < Date.now()) {
|
||||
console.log("Connection to server hung...");
|
||||
}
|
||||
}, 3500 )
|
||||
webSocket.onopen = (e) => {
|
||||
webSocket.send("TEST");
|
||||
}
|
||||
webSocket.onclose = (e) => {
|
||||
console.log("Disconnected from server");
|
||||
}
|
||||
webSocket.onerror = (e) => {
|
||||
console.log("Connection ERROR");
|
||||
console.log(e);
|
||||
}
|
||||
webSocket.onmessage = (e) => {
|
||||
if( e.data === "PONG" ) { last_contact = Date.now(); return }
|
||||
if( e.data === "TEST" ) { return console.log("Connected to server"); }
|
||||
console.log(e.data);
|
||||
console.log(JSON.parse(e.data));
|
||||
}
|
||||
window.setInterval( ()=>webSocket.send('PING'), 1000 );
|
||||
// })
|
||||
Reference in New Issue
Block a user