yuntian-deng commited on
Commit
1679b8f
·
1 Parent(s): 9c4ee1e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -2
main.py CHANGED
@@ -54,6 +54,8 @@ def predict_next_frame(previous_frames: List[np.ndarray], previous_actions: List
54
  # WebSocket endpoint for continuous user interaction
55
  @app.websocket("/ws")
56
  async def websocket_endpoint(websocket: WebSocket):
 
 
57
  await websocket.accept()
58
  previous_frames = []
59
  previous_actions = []
@@ -63,6 +65,11 @@ async def websocket_endpoint(websocket: WebSocket):
63
  try:
64
  # Receive user input with a timeout
65
  data = await asyncio.wait_for(websocket.receive_json(), timeout=30.0)
 
 
 
 
 
66
  action_type = data.get("action_type")
67
  mouse_position = data.get("mouse_position")
68
 
@@ -91,8 +98,8 @@ async def websocket_endpoint(websocket: WebSocket):
91
  break
92
 
93
  except Exception as e:
94
- print(f"Error in WebSocket connection: {e}")
95
 
96
  finally:
97
- print("WebSocket connection closed")
98
  # Remove the explicit websocket.close() call here
 
54
  # WebSocket endpoint for continuous user interaction
55
  @app.websocket("/ws")
56
  async def websocket_endpoint(websocket: WebSocket):
57
+ client_id = id(websocket) # Use a unique identifier for each connection
58
+ print(f"New WebSocket connection: {client_id}")
59
  await websocket.accept()
60
  previous_frames = []
61
  previous_actions = []
 
65
  try:
66
  # Receive user input with a timeout
67
  data = await asyncio.wait_for(websocket.receive_json(), timeout=30.0)
68
+
69
+ if data.get("type") == "heartbeat":
70
+ await websocket.send_json({"type": "heartbeat_response"})
71
+ continue
72
+
73
  action_type = data.get("action_type")
74
  mouse_position = data.get("mouse_position")
75
 
 
98
  break
99
 
100
  except Exception as e:
101
+ print(f"Error in WebSocket connection {client_id}: {e}")
102
 
103
  finally:
104
+ print(f"WebSocket connection closed: {client_id}")
105
  # Remove the explicit websocket.close() call here