@@ -12,7 +12,7 @@ namespace PuppeteerSharp
1212{
1313 /// <summary>
1414 /// The CDPSession instances are used to talk raw Chrome Devtools Protocol:
15- /// * Protocol methods can be called with <see cref="CDPSession.SendAsync(string, dynamic)"/> method.
15+ /// * Protocol methods can be called with <see cref="CDPSession.SendAsync(string, dynamic, bool )"/> method.
1616 /// * Protocol events, using the <see cref="CDPSession.MessageReceived"/> event.
1717 ///
1818 /// Documentation on DevTools Protocol can be found here: <see href="https://chromedevtools.github.io/devtools-protocol/"/>.
@@ -95,6 +95,10 @@ internal CDPSession(IConnection connection, TargetType targetType, string sessio
9595 #endregion
9696
9797 #region Public Methods
98+
99+ internal void Send ( string method , dynamic args = null )
100+ => _ = SendAsync ( method , args , false ) ;
101+
98102 /// <summary>
99103 /// Protocol methods can be called with this method.
100104 /// </summary>
@@ -113,9 +117,13 @@ public async Task<T> SendAsync<T>(string method, dynamic args = null)
113117 /// </summary>
114118 /// <param name="method">The method name</param>
115119 /// <param name="args">The method args</param>
120+ /// <param name="waitForCallback">
121+ /// If <c>true</c> the method will return a task to be completed when the message is confirmed by Chromium.
122+ /// If <c>false</c> the task will be considered complete after sending the message to Chromium.
123+ /// </param>
116124 /// <returns>The task.</returns>
117- /// <exception cref="T: PuppeteerSharp.PuppeteerException"></exception>
118- public async Task < JObject > SendAsync ( string method , dynamic args = null )
125+ /// <exception cref="PuppeteerSharp.PuppeteerException"></exception>
126+ public async Task < JObject > SendAsync ( string method , dynamic args = null , bool waitForCallback = true )
119127 {
120128 if ( Connection == null )
121129 {
@@ -130,31 +138,37 @@ public async Task<JObject> SendAsync(string method, dynamic args = null)
130138 } ) ;
131139 _logger . LogTrace ( "Send ► {Id} Method {Method} Params {@Params}" , id , method , ( object ) args ) ;
132140
133- var callback = new MessageTask
141+ MessageTask callback = null ;
142+ if ( waitForCallback )
134143 {
135- TaskWrapper = new TaskCompletionSource < JObject > ( ) ,
136- Method = method
137- } ;
138- _callbacks [ id ] = callback ;
144+ callback = new MessageTask
145+ {
146+ TaskWrapper = new TaskCompletionSource < JObject > ( ) ,
147+ Method = method
148+ } ;
149+ _callbacks [ id ] = callback ;
150+ }
139151
140152 try
141153 {
142- await Connection . SendAsync ( "Target.sendMessageToTarget" , new Dictionary < string , object >
143- {
144- { MessageKeys . SessionId , SessionId } ,
145- { MessageKeys . Message , message }
146- } ) . ConfigureAwait ( false ) ;
154+ await Connection . SendAsync (
155+ "Target.sendMessageToTarget" , new Dictionary < string , object >
156+ {
157+ { "sessionId" , SessionId } ,
158+ { "message" , message }
159+ } ,
160+ waitForCallback ) . ConfigureAwait ( false ) ;
147161 }
148162 catch ( Exception ex )
149163 {
150- if ( _callbacks . ContainsKey ( id ) )
164+ if ( waitForCallback && _callbacks . ContainsKey ( id ) )
151165 {
152166 _callbacks . Remove ( id ) ;
153167 callback . TaskWrapper . SetException ( new MessageException ( ex . Message , ex ) ) ;
154168 }
155169 }
156170
157- return await callback . TaskWrapper . Task . ConfigureAwait ( false ) ;
171+ return waitForCallback ? await callback . TaskWrapper . Task . ConfigureAwait ( false ) : null ;
158172 }
159173
160174 /// <summary>
@@ -281,7 +295,8 @@ internal CDPSession CreateSession(TargetType targetType, string sessionId)
281295 #region IConnection
282296 ILoggerFactory IConnection . LoggerFactory => LoggerFactory ;
283297 bool IConnection . IsClosed => IsClosed ;
284- Task < JObject > IConnection . SendAsync ( string method , dynamic args ) => SendAsync ( method , args ) ;
298+ Task < JObject > IConnection . SendAsync ( string method , dynamic args , bool waitForCallback )
299+ => SendAsync ( method , args , waitForCallback ) ;
285300 #endregion
286301 }
287302}
0 commit comments