- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10
Events
        CheshireCaat edited this page Sep 9, 2023 
        ·
        11 revisions
      
    This library uses pyee - port of NodeJS EventEmitter with some improvements for Python.
You can subscribe to certain events that come from the script. Events can be very different - new messages in the log, notifications about the start and stop of threads and browsers etc.
To do this, you can add event handler using some options to choose from:
- Add handler using method client.once('message_received', callback_fn).
- Add handler using method client.on('message_received', callback_fn).
- Add handler using methods described above as decorators for functions.
Take a look at the example:
import asyncio
from bas_remote import BasRemoteClient, Options
async def main():
    client = BasRemoteClient(Options(script_name='TestRemoteControl'))
    def callback1(message):
        # Process only log event.
        if message.type_ == 'log':
            # Output text to the console.
            print(f'[Log] - {message.data}')
    # Add event handler for log messages.
    client.on('message_received', callback1)
    def callback2(message):
        if message.type_ == 'thread_start':
            print('[Thread] - started')
        if message.type_ == 'thread_end':
            print('[Thread] - ended')
    # Add event handler for threads.
    client.on('message_received', callback2)
    await client.start()
    # Perform any actions here.
    await client.close()
if __name__ == '__main__':
    event_loop = asyncio.get_event_loop()
    event_loop.run_until_complete(main())