Skip to content
CheshireCaat edited this page Feb 27, 2020 · 3 revisions

Subscribe to script events

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 to client.OnMessageReceived:

using System;
using System.Threading.Tasks;
using BASRemote;

namespace BASRemoteExample
{
    internal static class Program
    {
        private static async Task Main()
        {
            var options = new Options {ScriptName = "AnyScriptName"};
            using (var client = new BasRemoteClient(options))
            {
                // Add event handler for log messages.
                client.OnMessageReceived += (message) =>
                {
                    var type = message.Type;
                    var data = message.Data;
                    // Process only log event
                    if (type == "log")
                    {
                        // Retrieve log text.
                        var text = data["text"];
                 
                        // Output text to the console
                        Console.WriteLine($"[Log] - {text}");
                    }
                };

                // Add event handler for threads.
                client.OnMessageReceived += (message) =>
                {
                    var type = message.Type;
                    if (type == "thread_start")
                    {
                        Console.WriteLine("[Thread] - started");
                    }
                    if (type == "thread_end")
                    {
                        Console.WriteLine("[Thread] - ended");
                    }
                };

                await client.Start();
                await Task.Delay(TimeSpan.FromSeconds(60));
            }
        }
     }
}
Clone this wiki locally