a9 commited on
Commit
f160fbc
·
verified ·
1 Parent(s): 6ce8b8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -5
app.py CHANGED
@@ -10,10 +10,13 @@ app = FastAPI()
10
  # Fixed users in the system (0 is the only hardcoded user for now, assuming another is 1)
11
  VALID_USER_IDS = [0, 1]
12
  # User status tracking (only tracks last time seen, actual online status is handled by ConnectionManager)
13
- userLastOnlineTime: Dict[int, float] = {id: 0.0 for id in VALID_USER_IDS}
14
  # In-memory "database" using a list of dictionaries
15
  messages: List[Dict] = []
16
 
 
 
 
17
 
18
  class ConnectionManager:
19
  """Manages active WebSocket connections, mapping user_id to WebSocket."""
@@ -88,8 +91,8 @@ def addMessage(sender_id: int, data: Dict) -> Dict:
88
  new_message = {
89
  "id": new_message_id,
90
  "content": data.get("content", ""),
91
- "time_client": data.get("timestamp", time.time()),
92
- "time_server": time.time(),
93
  "sender_id": sender_id,
94
  "receiver_id": receiver_id, # Target receiver ID
95
  "status": status_tracker
@@ -159,7 +162,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
159
  return
160
 
161
  await manager.connect(user_id, websocket)
162
- userLastOnlineTime[user_id] = time.time() # Now officially "online"
163
  print(f"User {user_id} connected.")
164
 
165
  # 2. Initial Status Broadcast
@@ -247,7 +250,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
247
  except WebSocketDisconnect:
248
  # 4. Disconnection Handling
249
  manager.disconnect(user_id)
250
- userLastOnlineTime[user_id] = time.time()
251
  print(f"User {user_id} disconnected at {userLastOnlineTime[user_id]}.")
252
 
253
  # 5. Status Broadcast on Disconnect
 
10
  # Fixed users in the system (0 is the only hardcoded user for now, assuming another is 1)
11
  VALID_USER_IDS = [0, 1]
12
  # User status tracking (only tracks last time seen, actual online status is handled by ConnectionManager)
13
+ userLastOnlineTime: Dict[int, int] = {id: 0 for id in VALID_USER_IDS}
14
  # In-memory "database" using a list of dictionaries
15
  messages: List[Dict] = []
16
 
17
+ async def timeMiliSec(sec_time):
18
+ return int(sec_time * 1000)
19
+
20
 
21
  class ConnectionManager:
22
  """Manages active WebSocket connections, mapping user_id to WebSocket."""
 
91
  new_message = {
92
  "id": new_message_id,
93
  "content": data.get("content", ""),
94
+ "time_client": data.get("timestamp", timeMiliSec(time.time())),
95
+ "time_server": timeMiliSec(time.time()),
96
  "sender_id": sender_id,
97
  "receiver_id": receiver_id, # Target receiver ID
98
  "status": status_tracker
 
162
  return
163
 
164
  await manager.connect(user_id, websocket)
165
+ userLastOnlineTime[user_id] = timeMiliSec(time.time()) # Now officially "online"
166
  print(f"User {user_id} connected.")
167
 
168
  # 2. Initial Status Broadcast
 
250
  except WebSocketDisconnect:
251
  # 4. Disconnection Handling
252
  manager.disconnect(user_id)
253
+ userLastOnlineTime[user_id] = timeMiliSec(time.time())
254
  print(f"User {user_id} disconnected at {userLastOnlineTime[user_id]}.")
255
 
256
  # 5. Status Broadcast on Disconnect