fix async issues

This commit is contained in:
liamcottle
2024-04-29 23:43:28 +12:00
parent 1a7baffe28
commit e5410517bc

22
web.py
View File

@@ -125,7 +125,7 @@ class ReticulumWebChat:
self.websocket_clients.append(client) self.websocket_clients.append(client)
# send config to all clients # send config to all clients
self.send_config_to_websocket_clients() await self.send_config_to_websocket_clients()
# handle client messages until disconnected # handle client messages until disconnected
while True: while True:
@@ -165,7 +165,7 @@ class ReticulumWebChat:
self.save_config() self.save_config()
# send config to websocket clients # send config to websocket clients
self.send_config_to_websocket_clients() await self.send_config_to_websocket_clients()
# handle sending an lxmf message # handle sending an lxmf message
elif _type == "lxmf.delivery": elif _type == "lxmf.delivery":
@@ -191,13 +191,13 @@ class ReticulumWebChat:
print("unhandled client message type: " + _type) print("unhandled client message type: " + _type)
# broadcast provided data to all connected websocket clients # broadcast provided data to all connected websocket clients
def websocket_broadcast(self, data): async def websocket_broadcast(self, data):
for websocket_client in self.websocket_clients: for websocket_client in self.websocket_clients:
asyncio.create_task(websocket_client.send(data)) await websocket_client.send(data)
# broadcasts config to all websocket clients # broadcasts config to all websocket clients
def send_config_to_websocket_clients(self): async def send_config_to_websocket_clients(self):
self.websocket_broadcast(json.dumps({ await self.websocket_broadcast(json.dumps({
"type": "config", "type": "config",
"config": { "config": {
"display_name": self.display_name, "display_name": self.display_name,
@@ -207,6 +207,7 @@ class ReticulumWebChat:
})) }))
# handle an lxmf delivery from reticulum # handle an lxmf delivery from reticulum
# NOTE: cant be async, as Reticulum doesn't await it
def on_lxmf_delivery(self, message): def on_lxmf_delivery(self, message):
try: try:
@@ -246,14 +247,14 @@ class ReticulumWebChat:
} }
# send received lxmf message data to all websocket clients # send received lxmf message data to all websocket clients
self.websocket_broadcast(json.dumps({ asyncio.run(self.websocket_broadcast(json.dumps({
"type": "lxmf.delivery", "type": "lxmf.delivery",
"source_hash": source_hash_text, "source_hash": source_hash_text,
"message": { "message": {
"content": message_content, "content": message_content,
"fields": fields, "fields": fields,
}, },
})) })))
except Exception as e: except Exception as e:
# do nothing on error # do nothing on error
@@ -292,6 +293,7 @@ class ReticulumWebChat:
print("failed to send lxmf message") print("failed to send lxmf message")
# handle an announce received from reticulum, for an lxmf address # handle an announce received from reticulum, for an lxmf address
# NOTE: cant be async, as Reticulum doesn't await it
def on_lxmf_announce_received(self, destination_hash, announced_identity, app_data): def on_lxmf_announce_received(self, destination_hash, announced_identity, app_data):
# log received announce # log received announce
@@ -303,11 +305,11 @@ class ReticulumWebChat:
parsed_app_data = app_data.decode("utf-8") parsed_app_data = app_data.decode("utf-8")
# send received lxmf announce to all websocket clients # send received lxmf announce to all websocket clients
self.websocket_broadcast(json.dumps({ asyncio.run(self.websocket_broadcast(json.dumps({
"type": "announce", "type": "announce",
"destination_hash": destination_hash.hex(), "destination_hash": destination_hash.hex(),
"app_data": parsed_app_data, "app_data": parsed_app_data,
})) })))
# an announce handler for lxmf.delivery aspect that just forwards to a provided callback # an announce handler for lxmf.delivery aspect that just forwards to a provided callback