feat(server): smaller websocket messages

This commit is contained in:
Laureηt 2022-02-08 18:45:01 +01:00
parent 3483d1e70c
commit 66f4e1fe05
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
2 changed files with 5 additions and 6 deletions

View file

@ -45,10 +45,10 @@ async def parse_message(user: User, message: websockets.typing.Data) -> None:
if user.last_message + USER_TIMEOUT > time.time():
logging.debug(f"dropping action: {message!r} from {user}")
return None
elif message in KEYS_ID:
r.incr(message)
elif (msg := KEYS_ID[int(message)]) in KEYS_ID:
r.incr(msg)
user.last_message = time.time()
logging.debug(f"received action: {message!r} from {user}")
logging.debug(f"received action: {msg} from {user}")
else:
logging.error(f"unsupported action: {message!r} from {user}")
@ -63,7 +63,6 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol, path: st
# Register user
user = User(websocket)
USERS.register(user)
logging.debug(f"registered user {user}")
try: # Manage received messages
async for message in user.websocket:
@ -74,11 +73,11 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol, path: st
logging.error(f"two coroutines called recv() concurrently, user={user}")
finally:
USERS.unregister(user)
logging.debug(f"unregistered user {user}")
async def main():
"""Start the websocket server."""
logging.debug("Server started !")
async with websockets.serve(handler, WEBSOCKET_SERVE, WEBSOCKET_PORT): # nosec
await asyncio.Future() # run forever

View file

@ -60,7 +60,7 @@ class Users(set):
user (User): the user to unregister.
"""
self.remove(user)
logging.debug(f"user unregistered: {self}")
logging.debug(f"user unregistered: {user}")
async def save(core):