Ejemplo n.º 1
0
 public void SerializeHive(ElementHive hive)
 {
     foreach (var kvp in hive.Components)
     {
         SerializeComponent(kvp.Key, kvp.Value);
     }
 }
Ejemplo n.º 2
0
        public async Task ExecuteAsync(Uri uri)
        {
            string circuitId = await GetPrerenderedCircuitId(uri);

            var builder = new HubConnectionBuilder();

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <IHubProtocol, IgnitorMessagePackHubProtocol>());
            builder.WithUrl(new Uri(uri, "_blazor/"));
            builder.ConfigureLogging(l => l.AddConsole().SetMinimumLevel(LogLevel.Trace));
            var hive = new ElementHive();

            await using var connection = builder.Build();
            await connection.StartAsync(CancellationToken);

            Console.WriteLine("Connected");

            connection.On <int, string, string>("JS.BeginInvokeJS", OnBeginInvokeJS);
            connection.On <int, int, byte[]>("JS.RenderBatch", OnRenderBatch);
            connection.On <Error>("JS.OnError", OnError);
            connection.Closed += OnClosedAsync;

            // Now everything is registered so we can start the circuit.
            var success = await connection.InvokeAsync <bool>("ConnectCircuit", circuitId);

            await TaskCompletionSource.Task;

            void OnBeginInvokeJS(int asyncHandle, string identifier, string argsJson)
            {
                Console.WriteLine("JS Invoke: " + identifier + " (" + argsJson + ")");
            }

            void OnRenderBatch(int browserRendererId, int batchId, byte[] batchData)
            {
                var batch = RenderBatchReader.Read(batchData);

                hive.Update(batch);

                // This will click the Counter component repeatedly resulting in infinite requests.
                _ = ClickAsync("thecounter", hive, connection);
            }

            void OnError(Error error)
            {
                Console.WriteLine("ERROR: " + error.Stack);
            }

            Task OnClosedAsync(Exception ex)
            {
                if (ex == null)
                {
                    TaskCompletionSource.TrySetResult(null);
                }
                else
                {
                    TaskCompletionSource.TrySetException(ex);
                }

                return(Task.CompletedTask);
            }
        }
Ejemplo n.º 3
0
 public static string Serialize(ElementHive hive)
 {
     using (var writer = new StringWriter())
     {
         var serializer = new Serializer(writer);
         serializer.SerializeHive(hive);
         return(writer.ToString());
     }
 }
Ejemplo n.º 4
0
        private static async Task ClickAsync(string id, ElementHive hive, HubConnection connection)
        {
            if (!hive.TryFindElementById(id, out var elementNode))
            {
                Console.WriteLine("Could not find the counter to perform a click. Exiting.");
                return;
            }

            await elementNode.ClickAsync(connection);
        }
Ejemplo n.º 5
0
        public async Task ExecuteAsync(Uri uri)
        {
            var httpClient = new HttpClient();
            var response   = await httpClient.GetAsync(uri);

            var content = await response.Content.ReadAsStringAsync();

            // <!-- M.A.C.Component:{"circuitId":"CfDJ8KZCIaqnXmdF...PVd6VVzfnmc1","rendererId":"0","componentId":"0"} -->
            var match     = Regex.Match(content, $"{Regex.Escape("<!-- M.A.C.Component:")}(.+?){Regex.Escape(" -->")}");
            var json      = JsonDocument.Parse(match.Groups[1].Value);
            var circuitId = json.RootElement.GetProperty("circuitId").GetString();

            var builder = new HubConnectionBuilder();

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <IHubProtocol, IgnitorMessagePackHubProtocol>());
            builder.WithUrl(new Uri(uri, "_blazor/"));
            builder.ConfigureLogging(l => l.AddConsole().SetMinimumLevel(LogLevel.Trace));
            var hive = new ElementHive();

            await using var connection = builder.Build();
            await connection.StartAsync(CancellationToken);

            Console.WriteLine("Connected");

            connection.On <int, string, string>("JS.BeginInvokeJS", OnBeginInvokeJS);
            connection.On <int, int, byte[]>("JS.RenderBatch", OnRenderBatch);
            connection.On <Error>("JS.OnError", OnError);
            connection.Closed += OnClosedAsync;

            // Now everything is registered so we can start the circuit.
            var success = await connection.InvokeAsync <bool>("ConnectCircuit", circuitId);

            await TaskCompletionSource.Task;

            void OnBeginInvokeJS(int asyncHandle, string identifier, string argsJson)
            {
                Console.WriteLine("JS Invoke: " + identifier + " (" + argsJson + ")");
            }

            void OnRenderBatch(int browserRendererId, int batchId, byte[] batchData)
            {
                var batch = RenderBatchReader.Read(batchData);

                hive.Update(batch);

                // This will click the Counter component repeatedly resulting in infinite requests.
                _ = ClickAsync("thecounter", hive, connection);
            }

            void OnError(Error error)
            {
                Console.WriteLine("ERROR: " + error.Stack);
            }

            Task OnClosedAsync(Exception ex)
            {
                if (ex == null)
                {
                    TaskCompletionSource.TrySetResult(null);
                }
                else
                {
                    TaskCompletionSource.TrySetException(ex);
                }

                return(Task.CompletedTask);
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> ConnectAsync(Uri uri, bool prerendered)
        {
            var builder = new HubConnectionBuilder();

            builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton <IHubProtocol, IgnitorMessagePackHubProtocol>());
            builder.WithUrl(new Uri(uri, "_blazor/"));
            builder.ConfigureLogging(l => l.AddConsole().SetMinimumLevel(LogLevel.Trace));
            var hive = new ElementHive();

            HubConnection = builder.Build();
            await HubConnection.StartAsync(CancellationToken);

            Console.WriteLine("Connected");

            HubConnection.On <int, string, string>("JS.BeginInvokeJS", OnBeginInvokeJS);
            HubConnection.On <int, int, byte[]>("JS.RenderBatch", OnRenderBatch);
            HubConnection.On <Error>("JS.OnError", OnError);
            HubConnection.Closed += OnClosedAsync;

            // Now everything is registered so we can start the circuit.
            if (prerendered)
            {
                CircuitId = await GetPrerenderedCircuitIdAsync(uri);

                return(await HubConnection.InvokeAsync <bool>("ConnectCircuit", CircuitId));
            }
            else
            {
                CircuitId = await HubConnection.InvokeAsync <string>("StartCircuit", uri, uri);

                return(CircuitId != null);
            }

            void OnBeginInvokeJS(int asyncHandle, string identifier, string argsJson)
            {
                JSInterop?.Invoke(asyncHandle, identifier, argsJson);
            }

            void OnRenderBatch(int browserRendererId, int batchId, byte[] batchData)
            {
                var batch = RenderBatchReader.Read(batchData);

                hive.Update(batch);

                if (ConfirmRenderBatch)
                {
                    HubConnection.InvokeAsync("OnRenderCompleted", batchId, /* error */ null);
                }

                RenderBatchReceived?.Invoke(browserRendererId, batchId, batchData);
            }

            void OnError(Error error)
            {
                Console.WriteLine("ERROR: " + error.Stack);
            }

            Task OnClosedAsync(Exception ex)
            {
                if (ex == null)
                {
                    TaskCompletionSource.TrySetResult(null);
                }
                else
                {
                    TaskCompletionSource.TrySetException(ex);
                }

                return(Task.CompletedTask);
            }
        }