Newer
Older
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MasterDevs.ChromeDevTools
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
public class ChromeProcess : IChromeProcess
{
public DirectoryInfo UserDirectory { get; set; }
public Process Process { get; set; }
public string RemoteDebuggingUri { get; set; }
public void Dispose()
{
Process.Kill();
try
{
UserDirectory.Delete(true);
}
catch
{
Thread.Sleep(500); // i'm being lazy because i'm tired
UserDirectory.Delete(true);
}
}
public async Task<string[]> GetSessions()
{
var remoteSessionUrls = new List<string>();
var webClient = new WebClient();
var uriBuilder = new UriBuilder(RemoteDebuggingUri);
uriBuilder.Path = "/json";
var remoteSessions = await webClient.DownloadStringTaskAsync(uriBuilder.Uri);
using(var stringReader = new StringReader(remoteSessions))
using(var jsonReader = new JsonTextReader(stringReader))
{
var sessionsObject = JToken.ReadFrom(jsonReader) as JArray;
foreach(var sessionObject in sessionsObject)
{
var sessionUrl = sessionObject["webSocketDebuggerUrl"].GetSafeString();
if(!String.IsNullOrEmpty(sessionUrl))
{
remoteSessionUrls.Add(sessionUrl);
}
}
}
return remoteSessionUrls.ToArray();
}
}
}