/// <summary> /// Protocol methods can be called with this method. /// </summary> /// <param name="method">The method name</param> /// <param name="args">The method args</param> /// <param name="waitForCallback"> /// If <c>true</c> the method will return a task to be completed when the message is confirmed by Chromium. /// If <c>false</c> the task will be considered complete after sending the message to Chromium. /// </param> /// <returns>The task.</returns> /// <exception cref="PuppeteerSharp.PuppeteerException">If the <see cref="Connection"/> is closed.</exception> public async Task <JObject> SendAsync(string method, object args = null, bool waitForCallback = true) { if (Connection == null) { throw new PuppeteerException( $"Protocol error ({method}): Session closed. " + $"Most likely the {TargetType} has been closed." + $"Close reason: {CloseReason}"); } var id = Connection.GetMessageID(); MessageTask callback = null; if (waitForCallback) { callback = new MessageTask { TaskWrapper = new TaskCompletionSource <JObject>(TaskCreationOptions.RunContinuationsAsynchronously), Method = method }; _callbacks[id] = callback; } try { await Connection.RawSendASync(id, method, args, SessionId).ConfigureAwait(false); } catch (Exception ex) { if (waitForCallback && _callbacks.TryRemove(id, out _)) { callback.TaskWrapper.TrySetException(new MessageException(ex.Message, ex)); } } return(waitForCallback ? await callback.TaskWrapper.Task.ConfigureAwait(false) : null); }
internal MessageException(MessageTask callback, ConnectionError error) : base(GetCallbackMessage(callback, error)) { }
internal MessageException(MessageTask callback, JObject obj) : base(GetCallbackMessage(callback, obj)) { }