da03 commited on
Commit
c629f7a
·
1 Parent(s): f1099a0
Files changed (2) hide show
  1. main.py +4 -5
  2. static/index.html +29 -14
main.py CHANGED
@@ -152,9 +152,8 @@ async def websocket_endpoint(websocket: WebSocket):
152
  await websocket.accept()
153
  previous_frames = []
154
  previous_actions = []
155
- positions = ['496~61', '815~335', '815~335', '815~335', '787~342', '749~345', '749~345', '703~346', '703~346', '654~347', '604~349', '604~349', '555~353', '509~357', '509~357']
156
  positions = ['815~335', '787~342', '787~342', '749~345', '703~346', '703~346', '654~347', '654~347', '604~349', '555~353', '555~353', '509~357', '509~357', '468~362', '431~368', '431~368']
157
- #positions = positions[:4]
158
  try:
159
  while True:
160
  try:
@@ -199,15 +198,15 @@ async def websocket_endpoint(websocket: WebSocket):
199
 
200
  except asyncio.TimeoutError:
201
  print("WebSocket connection timed out")
202
- #break
203
 
204
  except WebSocketDisconnect:
205
  print("WebSocket disconnected")
206
- #break
207
 
208
  except Exception as e:
209
  print(f"Error in WebSocket connection {client_id}: {e}")
210
 
211
  finally:
212
  print(f"WebSocket connection closed: {client_id}")
213
- # Remove the explicit websocket.close() call here
 
152
  await websocket.accept()
153
  previous_frames = []
154
  previous_actions = []
 
155
  positions = ['815~335', '787~342', '787~342', '749~345', '703~346', '703~346', '654~347', '654~347', '604~349', '555~353', '555~353', '509~357', '509~357', '468~362', '431~368', '431~368']
156
+
157
  try:
158
  while True:
159
  try:
 
198
 
199
  except asyncio.TimeoutError:
200
  print("WebSocket connection timed out")
201
+ break # Exit the loop on timeout
202
 
203
  except WebSocketDisconnect:
204
  print("WebSocket disconnected")
205
+ break # Exit the loop on disconnect
206
 
207
  except Exception as e:
208
  print(f"Error in WebSocket connection {client_id}: {e}")
209
 
210
  finally:
211
  print(f"WebSocket connection closed: {client_id}")
212
+ await websocket.close() # Ensure the WebSocket is closed
static/index.html CHANGED
@@ -66,7 +66,11 @@
66
  function startHeartbeat() {
67
  heartbeatInterval = setInterval(() => {
68
  if (isConnected) {
69
- socket.send(JSON.stringify({ type: "heartbeat" }));
 
 
 
 
70
  }
71
  }, 15000); // Send heartbeat every 15 seconds
72
  }
@@ -81,13 +85,16 @@
81
  function sendMousePosition(x, y, forceUpdate = false) {
82
  const currentTime = Date.now();
83
  if (isConnected && !isProcessing && (forceUpdate || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL)) {
84
- //isProcessing = true;
85
- socket.send(JSON.stringify({
86
- "action_type": "move",
87
- "mouse_position": [x, y]
88
- }));
89
- lastSentPosition = { x, y };
90
- lastSentTime = currentTime;
 
 
 
91
  }
92
  }
93
 
@@ -114,17 +121,25 @@
114
  let y = event.clientY - rect.top;
115
 
116
  isProcessing = true;
117
- socket.send(JSON.stringify({
118
- "action_type": "left_click",
119
- "mouse_position": [x, y]
120
- }));
 
 
 
 
121
  });
122
 
123
  // Graceful disconnection
124
  window.addEventListener('beforeunload', function (e) {
125
  if (isConnected) {
126
- socket.send(JSON.stringify({ type: "disconnect" }));
127
- socket.close();
 
 
 
 
128
  }
129
  });
130
  </script>
 
66
  function startHeartbeat() {
67
  heartbeatInterval = setInterval(() => {
68
  if (isConnected) {
69
+ try {
70
+ socket.send(JSON.stringify({ type: "heartbeat" }));
71
+ } catch (error) {
72
+ console.error("Error sending heartbeat:", error);
73
+ }
74
  }
75
  }, 15000); // Send heartbeat every 15 seconds
76
  }
 
85
  function sendMousePosition(x, y, forceUpdate = false) {
86
  const currentTime = Date.now();
87
  if (isConnected && !isProcessing && (forceUpdate || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL)) {
88
+ try {
89
+ socket.send(JSON.stringify({
90
+ "action_type": "move",
91
+ "mouse_position": [x, y]
92
+ }));
93
+ lastSentPosition = { x, y };
94
+ lastSentTime = currentTime;
95
+ } catch (error) {
96
+ console.error("Error sending mouse position:", error);
97
+ }
98
  }
99
  }
100
 
 
121
  let y = event.clientY - rect.top;
122
 
123
  isProcessing = true;
124
+ try {
125
+ socket.send(JSON.stringify({
126
+ "action_type": "left_click",
127
+ "mouse_position": [x, y]
128
+ }));
129
+ } catch (error) {
130
+ console.error("Error sending click action:", error);
131
+ }
132
  });
133
 
134
  // Graceful disconnection
135
  window.addEventListener('beforeunload', function (e) {
136
  if (isConnected) {
137
+ try {
138
+ socket.send(JSON.stringify({ type: "disconnect" }));
139
+ socket.close();
140
+ } catch (error) {
141
+ console.error("Error during disconnection:", error);
142
+ }
143
  }
144
  });
145
  </script>