da03 commited on
Commit
2225158
·
1 Parent(s): 21002d0
Files changed (2) hide show
  1. main.py +9 -0
  2. static/index.html +10 -4
main.py CHANGED
@@ -403,6 +403,15 @@ async def websocket_endpoint(websocket: WebSocket):
403
  # Close connection at 1 minute
404
  if time_since_activity >= CONNECTION_TIMEOUT:
405
  print(f"[{current_time:.3f}] Closing connection {client_id} due to timeout")
 
 
 
 
 
 
 
 
 
406
  await websocket.close(code=1000, reason="User inactivity timeout")
407
  return
408
 
 
403
  # Close connection at 1 minute
404
  if time_since_activity >= CONNECTION_TIMEOUT:
405
  print(f"[{current_time:.3f}] Closing connection {client_id} due to timeout")
406
+
407
+ # Clear the input queue before closing
408
+ while not input_queue.empty():
409
+ try:
410
+ input_queue.get_nowait()
411
+ input_queue.task_done()
412
+ except asyncio.QueueEmpty:
413
+ break
414
+
415
  await websocket.close(code=1000, reason="User inactivity timeout")
416
  return
417
 
static/index.html CHANGED
@@ -293,7 +293,7 @@
293
  autoInputInterval = setInterval(() => {
294
  const currentTime = Date.now();
295
 
296
- if (!autoInputEnabled || !lastSentPosition || !isConnected) {
297
  return;
298
  }
299
 
@@ -330,6 +330,8 @@
330
  lastAutoInputTime = currentTime;
331
  } catch (error) {
332
  console.error("Error sending auto-input:", error);
 
 
333
  }
334
  }
335
  }, 100); // Check every 100ms
@@ -410,14 +412,18 @@
410
  function resetTimeout() {
411
  // Send a heartbeat to reset the server's timeout
412
  if (socket && socket.readyState === WebSocket.OPEN) {
413
- socket.send(JSON.stringify({ type: "heartbeat" }));
 
 
 
 
414
  }
415
  stopTimeoutCountdown();
416
  }
417
 
418
  function sendInputState(x, y, isLeftClick = false, isRightClick = false) {
419
  const currentTime = Date.now();
420
- if (isConnected && (isLeftClick || isRightClick || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL)) {
421
  try {
422
  socket.send(JSON.stringify({
423
  "x": x,
@@ -497,7 +503,7 @@
497
  });
498
 
499
  document.addEventListener("keyup", function (event) {
500
- if (!isConnected) return;
501
 
502
  // Remove the key from our set of pressed keys
503
  pressedKeys.delete(event.key);
 
293
  autoInputInterval = setInterval(() => {
294
  const currentTime = Date.now();
295
 
296
+ if (!autoInputEnabled || !lastSentPosition || !isConnected || socket.readyState !== WebSocket.OPEN) {
297
  return;
298
  }
299
 
 
330
  lastAutoInputTime = currentTime;
331
  } catch (error) {
332
  console.error("Error sending auto-input:", error);
333
+ // Stop auto-input if there's an error (connection likely closed)
334
+ stopAutoInput();
335
  }
336
  }
337
  }, 100); // Check every 100ms
 
412
  function resetTimeout() {
413
  // Send a heartbeat to reset the server's timeout
414
  if (socket && socket.readyState === WebSocket.OPEN) {
415
+ try {
416
+ socket.send(JSON.stringify({ type: "heartbeat" }));
417
+ } catch (error) {
418
+ console.error("Error sending heartbeat:", error);
419
+ }
420
  }
421
  stopTimeoutCountdown();
422
  }
423
 
424
  function sendInputState(x, y, isLeftClick = false, isRightClick = false) {
425
  const currentTime = Date.now();
426
+ if (isConnected && socket.readyState === WebSocket.OPEN && (isLeftClick || isRightClick || !lastSentPosition || currentTime - lastSentTime >= SEND_INTERVAL)) {
427
  try {
428
  socket.send(JSON.stringify({
429
  "x": x,
 
503
  });
504
 
505
  document.addEventListener("keyup", function (event) {
506
+ if (!isConnected || socket.readyState !== WebSocket.OPEN) return;
507
 
508
  // Remove the key from our set of pressed keys
509
  pressedKeys.delete(event.key);