yuntian-deng commited on
Commit
f70fe7a
·
1 Parent(s): e62ac65

Update static/index.html

Browse files
Files changed (1) hide show
  1. static/index.html +25 -4
static/index.html CHANGED
@@ -51,6 +51,22 @@
51
  // Initial connection
52
  connect();
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  // Capture mouse movements and clicks
55
  canvas.addEventListener("mousemove", function (event) {
56
  if (!isConnected) return;
@@ -58,10 +74,13 @@
58
  let x = event.clientX - rect.left;
59
  let y = event.clientY - rect.top;
60
 
61
- socket.send(JSON.stringify({
62
- "action_type": "move",
63
- "mouse_position": [x, y]
64
- }));
 
 
 
65
  });
66
 
67
  canvas.addEventListener("click", function (event) {
@@ -70,6 +89,8 @@
70
  let x = event.clientX - rect.left;
71
  let y = event.clientY - rect.top;
72
 
 
 
73
  socket.send(JSON.stringify({
74
  "action_type": "left_click",
75
  "mouse_position": [x, y]
 
51
  // Initial connection
52
  connect();
53
 
54
+ let lastSentPosition = null;
55
+ let lastSentTime = 0;
56
+ const SEND_INTERVAL = 50; // Send updates every 50ms
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]
64
+ }));
65
+ lastSentPosition = { x, y };
66
+ lastSentTime = currentTime;
67
+ }
68
+ }
69
+
70
  // Capture mouse movements and clicks
71
  canvas.addEventListener("mousemove", function (event) {
72
  if (!isConnected) return;
 
74
  let x = event.clientX - rect.left;
75
  let y = event.clientY - rect.top;
76
 
77
+ // Client-side prediction
78
+ ctx.beginPath();
79
+ ctx.moveTo(lastSentPosition ? lastSentPosition.x : x, lastSentPosition ? lastSentPosition.y : y);
80
+ ctx.lineTo(x, y);
81
+ ctx.stroke();
82
+
83
+ sendMousePosition(x, y);
84
  });
85
 
86
  canvas.addEventListener("click", function (event) {
 
89
  let x = event.clientX - rect.left;
90
  let y = event.clientY - rect.top;
91
 
92
+ sendMousePosition(x, y, true);
93
+
94
  socket.send(JSON.stringify({
95
  "action_type": "left_click",
96
  "mouse_position": [x, y]