Refactoring, added docstrings
This commit is contained in:
@@ -18,6 +18,12 @@ class Room(object):
|
||||
connections: Dict[str, Connection] = {}
|
||||
|
||||
async def accept_connection(self, ws: WebSocket) -> Connection:
|
||||
"""
|
||||
Accepts connection, checks username availability and adds it to dict of
|
||||
connections
|
||||
:param ws: Websocket of connection
|
||||
:return:
|
||||
"""
|
||||
print('Incoming connection')
|
||||
await ws.accept()
|
||||
connection = Connection(
|
||||
@@ -36,11 +42,22 @@ class Room(object):
|
||||
return connection
|
||||
|
||||
async def broadcast_webmessage(self, obj: webmessages_union):
|
||||
"""
|
||||
Broadcasts WebMessages to all connections in room
|
||||
:param obj:
|
||||
:return:
|
||||
"""
|
||||
for connection in self.connections.values():
|
||||
print(f'Sending to {connection.username}: {obj}')
|
||||
await connection.send_webmessage(obj)
|
||||
|
||||
async def broadcast_message(self, from_username: str, message: str):
|
||||
"""
|
||||
Broadcasts message to every user in room
|
||||
:param from_username: User that sent message
|
||||
:param message: content
|
||||
:return:
|
||||
"""
|
||||
await self.broadcast_webmessage(
|
||||
WebMessageMessage(
|
||||
username=from_username,
|
||||
@@ -49,6 +66,11 @@ class Room(object):
|
||||
)
|
||||
|
||||
async def broadcast_notification(self, message: str):
|
||||
"""
|
||||
Broadcasts notification from server
|
||||
:param message: Content
|
||||
:return:
|
||||
"""
|
||||
await self.broadcast_webmessage(
|
||||
WebNotificationMessage(
|
||||
message=message
|
||||
@@ -59,6 +81,11 @@ class Room(object):
|
||||
self,
|
||||
error_message: webmessage_error_message_literal
|
||||
):
|
||||
"""
|
||||
Broadcasts server error
|
||||
:param error_message: See webmessage_error_message_literal
|
||||
:return:
|
||||
"""
|
||||
await self.broadcast_webmessage(
|
||||
WebErrorMessage(
|
||||
error_message=error_message
|
||||
@@ -66,6 +93,11 @@ class Room(object):
|
||||
)
|
||||
|
||||
async def broadcast_user_disconnected(self, username: str):
|
||||
"""
|
||||
Broadcasts that user is disconnected
|
||||
:param username: Username of user that disconnected
|
||||
:return:
|
||||
"""
|
||||
await self.broadcast_webmessage(
|
||||
WebUserMessage(
|
||||
type="disconnect",
|
||||
@@ -74,11 +106,24 @@ class Room(object):
|
||||
)
|
||||
|
||||
async def get_connection_by(self, attribute: str, value: str) -> Connection | None:
|
||||
"""
|
||||
Search for connection by attribute and value in it
|
||||
:param attribute:
|
||||
:param value:
|
||||
:return:
|
||||
"""
|
||||
for connection in self.connections.values():
|
||||
if getattr(connection, attribute) == value:
|
||||
return connection
|
||||
|
||||
async def disconnect(self, connection: Connection, close_reason: str | None = None):
|
||||
"""
|
||||
Disconnects by connection object.
|
||||
:param connection: Object of connection.
|
||||
It can be obtained using get_connection_by
|
||||
:param close_reason: Reason if exists
|
||||
:return:
|
||||
"""
|
||||
if connection not in self.connections.values():
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user