feat(server): smaller websocket messages
This commit is contained in:
parent
3483d1e70c
commit
66f4e1fe05
|
@ -45,10 +45,10 @@ async def parse_message(user: User, message: websockets.typing.Data) -> None:
|
||||||
if user.last_message + USER_TIMEOUT > time.time():
|
if user.last_message + USER_TIMEOUT > time.time():
|
||||||
logging.debug(f"dropping action: {message!r} from {user}")
|
logging.debug(f"dropping action: {message!r} from {user}")
|
||||||
return None
|
return None
|
||||||
elif message in KEYS_ID:
|
elif (msg := KEYS_ID[int(message)]) in KEYS_ID:
|
||||||
r.incr(message)
|
r.incr(msg)
|
||||||
user.last_message = time.time()
|
user.last_message = time.time()
|
||||||
logging.debug(f"received action: {message!r} from {user}")
|
logging.debug(f"received action: {msg} from {user}")
|
||||||
else:
|
else:
|
||||||
logging.error(f"unsupported action: {message!r} from {user}")
|
logging.error(f"unsupported action: {message!r} from {user}")
|
||||||
|
|
||||||
|
@ -63,7 +63,6 @@ async def handler(websocket: websockets.server.WebSocketServerProtocol, path: st
|
||||||
# Register user
|
# Register user
|
||||||
user = User(websocket)
|
user = User(websocket)
|
||||||
USERS.register(user)
|
USERS.register(user)
|
||||||
logging.debug(f"registered user {user}")
|
|
||||||
|
|
||||||
try: # Manage received messages
|
try: # Manage received messages
|
||||||
async for message in user.websocket:
|
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}")
|
logging.error(f"two coroutines called recv() concurrently, user={user}")
|
||||||
finally:
|
finally:
|
||||||
USERS.unregister(user)
|
USERS.unregister(user)
|
||||||
logging.debug(f"unregistered user {user}")
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
"""Start the websocket server."""
|
"""Start the websocket server."""
|
||||||
|
logging.debug("Server started !")
|
||||||
async with websockets.serve(handler, WEBSOCKET_SERVE, WEBSOCKET_PORT): # nosec
|
async with websockets.serve(handler, WEBSOCKET_SERVE, WEBSOCKET_PORT): # nosec
|
||||||
await asyncio.Future() # run forever
|
await asyncio.Future() # run forever
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ class Users(set):
|
||||||
user (User): the user to unregister.
|
user (User): the user to unregister.
|
||||||
"""
|
"""
|
||||||
self.remove(user)
|
self.remove(user)
|
||||||
logging.debug(f"user unregistered: {self}")
|
logging.debug(f"user unregistered: {user}")
|
||||||
|
|
||||||
|
|
||||||
async def save(core):
|
async def save(core):
|
||||||
|
|
Loading…
Reference in a new issue