@@ -13,6 +13,8 @@ async def main(room: rtc.Room):
1313 logging .basicConfig (level = logging .INFO )
1414 logger = logging .getLogger (__name__ )
1515
16+ active_tasks = []
17+
1618 async def greetParticipant (identity : str ):
1719 text_writer = await room .local_participant .stream_text (
1820 destination_identities = [identity ], topic = "chat"
@@ -32,8 +34,8 @@ async def on_chat_message_received(reader: rtc.TextStreamReader, participant_ide
3234 logger .info ("Received chat message from %s: '%s'" , participant_identity , full_text )
3335
3436 async def on_welcome_image_received (reader : rtc .ByteStreamReader , participant_identity : str ):
35- logger .info ("Received image from %s: '%s'" , participant_identity , reader .info [ " name" ] )
36- with open (reader .info [ " name" ] , mode = "wb" ) as f :
37+ logger .info ("Received image from %s: '%s'" , participant_identity , reader .info . name )
38+ with open (reader .info . name , mode = "wb" ) as f :
3739 async for chunk in reader :
3840 f .write (chunk )
3941
@@ -44,19 +46,19 @@ def on_participant_connected(participant: rtc.RemoteParticipant):
4446 logger .info ("participant connected: %s %s" , participant .sid , participant .identity )
4547 asyncio .create_task (greetParticipant (participant .identity ))
4648
47- room .set_text_stream_handler (
48- "chat" ,
49- lambda reader , participant_identity : asyncio .create_task (
50- on_chat_message_received (reader , participant_identity )
51- ),
52- )
49+ def _handle_chat_stream (reader , participant_identity ):
50+ task = asyncio .create_task (on_chat_message_received (reader , participant_identity ))
51+ active_tasks .append (task )
52+ task .add_done_callback (lambda _ : active_tasks .remove (task ))
5353
54- room .set_byte_stream_handler (
55- "files" ,
56- lambda reader , participant_identity : asyncio .create_task (
57- on_welcome_image_received (reader , participant_identity )
58- ),
59- )
54+ room .set_text_stream_handler ("chat" , _handle_chat_stream )
55+
56+ def _handle_welcome_image_stream (reader , participant_identity ):
57+ task = asyncio .create_task (on_welcome_image_received (reader , participant_identity ))
58+ active_tasks .append (task )
59+ task .add_done_callback (lambda _ : active_tasks .remove (task ))
60+
61+ room .set_byte_stream_handler ("files" , _handle_welcome_image_stream )
6062
6163 # By default, autosubscribe is enabled. The participant will be subscribed to
6264 # all published tracks in the room
0 commit comments