Skip to content

Commit 4f76a82

Browse files
authored
Implement Connection.Delay (#341)
1 parent edb38f5 commit 4f76a82

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

lib/PuppeteerSharp/Connection.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ internal Connection(string url, int delay, ClientWebSocket ws, ILoggerFactory lo
8787
internal async Task<dynamic> SendAsync(string method, dynamic args = null)
8888
{
8989
var id = ++_lastId;
90-
var message = JsonConvert.SerializeObject(new Dictionary<string, object>(){
90+
var message = JsonConvert.SerializeObject(new Dictionary<string, object>
91+
{
9192
{"id", id},
9293
{"method", method},
9394
{"params", args}
@@ -102,7 +103,7 @@ internal async Task<dynamic> SendAsync(string method, dynamic args = null)
102103
};
103104

104105
var encoded = Encoding.UTF8.GetBytes(message);
105-
var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
106+
var buffer = new ArraySegment<byte>(encoded, 0, encoded.Length);
106107
await _socketQueue.Enqueue(() => WebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, default(CancellationToken)));
107108

108109
if (method == CloseMessage)
@@ -169,7 +170,7 @@ private async Task<object> GetResponseAsync()
169170
}
170171

171172
var endOfMessage = false;
172-
string response = string.Empty;
173+
var response = string.Empty;
173174

174175
while (!endOfMessage)
175176
{
@@ -212,6 +213,11 @@ private async Task<object> GetResponseAsync()
212213

213214
if (!string.IsNullOrEmpty(response))
214215
{
216+
if (Delay > 0)
217+
{
218+
await Task.Delay(Delay);
219+
}
220+
215221
ProcessResponse(response);
216222
}
217223
}
@@ -226,7 +232,7 @@ private void ProcessResponse(string response)
226232

227233
if (objAsJObject["id"] != null)
228234
{
229-
int id = (int)objAsJObject["id"];
235+
var id = (int)objAsJObject["id"];
230236

231237
//If we get the object we are waiting for we return if
232238
//if not we add this to the list, sooner or later some one will come for it
@@ -294,4 +300,4 @@ public void Dispose()
294300

295301
#endregion
296302
}
297-
}
303+
}

lib/PuppeteerSharp/PuppeteerSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<PropertyGroup>
6767
<DocumentationFile>bin\$(Configuration)\netstandard2.0\PuppeteerSharp.xml</DocumentationFile>
6868
</PropertyGroup>
69+
<PropertyGroup>
70+
<LangVersion>7.1</LangVersion>
71+
</PropertyGroup>
6972
<ItemGroup>
7073
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.2" />
7174
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />

0 commit comments

Comments
 (0)