Commit da47c2df authored by Timothy J. Baek's avatar Timothy J. Baek
Browse files

refac: active users

parent 694d1708
...@@ -10,7 +10,7 @@ app = socketio.ASGIApp(sio, socketio_path="/ws/socket.io") ...@@ -10,7 +10,7 @@ app = socketio.ASGIApp(sio, socketio_path="/ws/socket.io")
# Dictionary to maintain the user pool # Dictionary to maintain the user pool
SESSION_POOL = {}
USER_POOL = {} USER_POOL = {}
USAGE_POOL = {} USAGE_POOL = {}
# Timeout duration in seconds # Timeout duration in seconds
...@@ -29,7 +29,12 @@ async def connect(sid, environ, auth): ...@@ -29,7 +29,12 @@ async def connect(sid, environ, auth):
user = Users.get_user_by_id(data["id"]) user = Users.get_user_by_id(data["id"])
if user: if user:
USER_POOL[sid] = user.id SESSION_POOL[sid] = user.id
if user.id in USER_POOL:
USER_POOL[user.id].append(sid)
else:
USER_POOL[user.id] = [sid]
print(f"user {user.name}({user.id}) connected with session ID {sid}") print(f"user {user.name}({user.id}) connected with session ID {sid}")
print(len(set(USER_POOL))) print(len(set(USER_POOL)))
...@@ -50,7 +55,13 @@ async def user_join(sid, data): ...@@ -50,7 +55,13 @@ async def user_join(sid, data):
user = Users.get_user_by_id(data["id"]) user = Users.get_user_by_id(data["id"])
if user: if user:
USER_POOL[sid] = user.id
SESSION_POOL[sid] = user.id
if user.id in USER_POOL:
USER_POOL[user.id].append(sid)
else:
USER_POOL[user.id] = [sid]
print(f"user {user.name}({user.id}) connected with session ID {sid}") print(f"user {user.name}({user.id}) connected with session ID {sid}")
print(len(set(USER_POOL))) print(len(set(USER_POOL)))
...@@ -124,7 +135,11 @@ async def remove_after_timeout(sid, model_id): ...@@ -124,7 +135,11 @@ async def remove_after_timeout(sid, model_id):
@sio.event @sio.event
async def disconnect(sid): async def disconnect(sid):
if sid in USER_POOL: if sid in USER_POOL:
disconnected_user = USER_POOL.pop(sid) disconnected_user = SESSION_POOL.pop(sid)
USER_POOL[disconnected_user].remove(sid)
if len(USER_POOL[disconnected_user]) == 0:
del USER_POOL[disconnected_user]
print(f"user {disconnected_user} disconnected with session ID {sid}") print(f"user {disconnected_user} disconnected with session ID {sid}")
await sio.emit("user-count", {"count": len(USER_POOL)}) await sio.emit("user-count", {"count": len(USER_POOL)})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment