28 lines
880 B
JavaScript
28 lines
880 B
JavaScript
// 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 );
|
|
// })
|