From 03a2d46e07a805b632e0f050a7cc27f11e3a088c Mon Sep 17 00:00:00 2001 From: liamcottle Date: Sun, 2 Jun 2024 12:07:59 +1200 Subject: [PATCH] send websocket event when we have sent an announce --- public/index.html | 5 +++++ web.py | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/public/index.html b/public/index.html index c6a65c9..b8b6a9a 100644 --- a/public/index.html +++ b/public/index.html @@ -1173,6 +1173,11 @@ } break; } + case 'announced': { + // we just announced, update config so we can show the new last updated at + this.getConfig(); + break; + } case 'lxmf.delivery': { // add inbound message to ui diff --git a/web.py b/web.py index 24aa032..3ee37f4 100644 --- a/web.py +++ b/web.py @@ -105,12 +105,12 @@ class ReticulumWebChat: self.audio_call_manager.register_incoming_call_callback(self.on_incoming_audio_call) # start background thread for auto announce loop - thread = threading.Thread(target=self.announce_loop) + thread = threading.Thread(target=asyncio.run, args=(self.announce_loop(),)) thread.daemon = True thread.start() # automatically announces based on user config - def announce_loop(self): + async def announce_loop(self): while True: should_announce = False @@ -136,10 +136,10 @@ class ReticulumWebChat: # announce if should_announce: - self.announce() + await self.announce() # wait 1 second before next loop - time.sleep(1) + await asyncio.sleep(1) # handle receiving a new audio call def on_incoming_audio_call(self, audio_call: AudioCall): @@ -927,7 +927,7 @@ class ReticulumWebChat: web.run_app(app, host=host, port=port) # handle announcing - def announce(self): + async def announce(self): # update last announced at timestamp self.config.last_announced_at.set(int(time.time())) @@ -938,6 +938,9 @@ class ReticulumWebChat: # send announce for audio call self.audio_call_manager.announce(app_data=self.config.display_name.get().encode("utf-8")) + # tell websocket clients we just announced + await self.send_announced_to_websocket_clients() + # handle data received from websocket client async def on_websocket_data_received(self, client, data): @@ -1009,7 +1012,7 @@ class ReticulumWebChat: # handle sending an announce elif _type == "announce": - self.announce() + await self.announce() # handle downloading a file from a nomadnet node elif _type == "nomadnet.file.download": @@ -1136,6 +1139,12 @@ class ReticulumWebChat: "config": self.get_config_dict(), })) + # broadcasts to all websocket clients that we just announced + async def send_announced_to_websocket_clients(self): + await self.websocket_broadcast(json.dumps({ + "type": "announced", + })) + # returns a dictionary of config def get_config_dict(self): return {