Spaces:
Runtime error
Runtime error
Commit
·
9c4ee1e
1
Parent(s):
a37042d
Update static/index.html
Browse files- static/index.html +34 -6
static/index.html
CHANGED
|
@@ -14,6 +14,8 @@
|
|
| 14 |
|
| 15 |
let socket;
|
| 16 |
let isConnected = false;
|
|
|
|
|
|
|
| 17 |
|
| 18 |
function connect() {
|
| 19 |
socket = new WebSocket(`wss://${window.location.host}/ws`);
|
|
@@ -21,24 +23,26 @@
|
|
| 21 |
socket.onopen = function(event) {
|
| 22 |
console.log("WebSocket connection established");
|
| 23 |
isConnected = true;
|
|
|
|
|
|
|
| 24 |
};
|
| 25 |
|
| 26 |
socket.onclose = function(event) {
|
| 27 |
console.log("WebSocket connection closed. Attempting to reconnect...");
|
| 28 |
isConnected = false;
|
| 29 |
-
|
|
|
|
| 30 |
};
|
| 31 |
|
| 32 |
socket.onerror = function(error) {
|
| 33 |
console.error("WebSocket error:", error);
|
| 34 |
};
|
| 35 |
|
| 36 |
-
// Receive predicted frames and render them on the canvas
|
| 37 |
socket.onmessage = function (event) {
|
| 38 |
-
// Parse the received JSON data
|
| 39 |
const data = JSON.parse(event.data);
|
| 40 |
-
if (data.
|
| 41 |
-
|
|
|
|
| 42 |
const img = new Image();
|
| 43 |
img.onload = function() {
|
| 44 |
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
@@ -48,6 +52,22 @@
|
|
| 48 |
};
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
// Initial connection
|
| 52 |
connect();
|
| 53 |
|
|
@@ -57,7 +77,7 @@
|
|
| 57 |
|
| 58 |
function sendMousePosition(x, y, forceUpdate = false) {
|
| 59 |
const currentTime = Date.now();
|
| 60 |
-
if (forceUpdate || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL) {
|
| 61 |
socket.send(JSON.stringify({
|
| 62 |
"action_type": "move",
|
| 63 |
"mouse_position": [x, y]
|
|
@@ -96,6 +116,14 @@
|
|
| 96 |
"mouse_position": [x, y]
|
| 97 |
}));
|
| 98 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
</script>
|
| 100 |
</body>
|
| 101 |
</html>
|
|
|
|
| 14 |
|
| 15 |
let socket;
|
| 16 |
let isConnected = false;
|
| 17 |
+
let reconnectAttempts = 0;
|
| 18 |
+
const MAX_RECONNECT_DELAY = 30000; // Maximum delay between reconnection attempts (30 seconds)
|
| 19 |
|
| 20 |
function connect() {
|
| 21 |
socket = new WebSocket(`wss://${window.location.host}/ws`);
|
|
|
|
| 23 |
socket.onopen = function(event) {
|
| 24 |
console.log("WebSocket connection established");
|
| 25 |
isConnected = true;
|
| 26 |
+
reconnectAttempts = 0;
|
| 27 |
+
startHeartbeat();
|
| 28 |
};
|
| 29 |
|
| 30 |
socket.onclose = function(event) {
|
| 31 |
console.log("WebSocket connection closed. Attempting to reconnect...");
|
| 32 |
isConnected = false;
|
| 33 |
+
clearInterval(heartbeatInterval);
|
| 34 |
+
scheduleReconnection();
|
| 35 |
};
|
| 36 |
|
| 37 |
socket.onerror = function(error) {
|
| 38 |
console.error("WebSocket error:", error);
|
| 39 |
};
|
| 40 |
|
|
|
|
| 41 |
socket.onmessage = function (event) {
|
|
|
|
| 42 |
const data = JSON.parse(event.data);
|
| 43 |
+
if (data.type === "heartbeat_response") {
|
| 44 |
+
console.log("Heartbeat response received");
|
| 45 |
+
} else if (data.image) {
|
| 46 |
const img = new Image();
|
| 47 |
img.onload = function() {
|
| 48 |
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
|
|
|
|
| 52 |
};
|
| 53 |
}
|
| 54 |
|
| 55 |
+
function scheduleReconnection() {
|
| 56 |
+
const delay = Math.min(1000 * Math.pow(2, reconnectAttempts), MAX_RECONNECT_DELAY);
|
| 57 |
+
console.log(`Scheduling reconnection in ${delay}ms`);
|
| 58 |
+
setTimeout(connect, delay);
|
| 59 |
+
reconnectAttempts++;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
let heartbeatInterval;
|
| 63 |
+
function startHeartbeat() {
|
| 64 |
+
heartbeatInterval = setInterval(() => {
|
| 65 |
+
if (isConnected) {
|
| 66 |
+
socket.send(JSON.stringify({ type: "heartbeat" }));
|
| 67 |
+
}
|
| 68 |
+
}, 15000); // Send heartbeat every 15 seconds
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
// Initial connection
|
| 72 |
connect();
|
| 73 |
|
|
|
|
| 77 |
|
| 78 |
function sendMousePosition(x, y, forceUpdate = false) {
|
| 79 |
const currentTime = Date.now();
|
| 80 |
+
if (isConnected && (forceUpdate || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL)) {
|
| 81 |
socket.send(JSON.stringify({
|
| 82 |
"action_type": "move",
|
| 83 |
"mouse_position": [x, y]
|
|
|
|
| 116 |
"mouse_position": [x, y]
|
| 117 |
}));
|
| 118 |
});
|
| 119 |
+
|
| 120 |
+
// Graceful disconnection
|
| 121 |
+
window.addEventListener('beforeunload', function (e) {
|
| 122 |
+
if (isConnected) {
|
| 123 |
+
socket.send(JSON.stringify({ type: "disconnect" }));
|
| 124 |
+
socket.close();
|
| 125 |
+
}
|
| 126 |
+
});
|
| 127 |
</script>
|
| 128 |
</body>
|
| 129 |
</html>
|