Tips and tricks for working with Mara
Each connection instance reads from its connection in its own async read loop, and
raises a Receive
event when data is received. This event is then handled within that
connection's read loop, blocking any data from that connection until the event is
completely handled.
This means that an event can effectively pause the read loop for that connection and
capture inbound content connection.read()
:
@app.on(events.Connect) async def login(event: events.Connect): event.connection.write("Username: ", end="") username: str = event.connection.read() event.connection.write(f"Welcome, {username}")
Here the connection.read()
will capture the first input from a user, and will not
trigger a Receive
event.
Note that connections can write during an event, as outgoing data is sent using a
separate async write loop. You may want to call await event.connection.flush()
to
ensure the data has been sent before continuing.