Skip to content
Snippets Groups Projects
Commit cb06f50c authored by Silas Kuder's avatar Silas Kuder
Browse files

Merge branch 'receiveMultipleMessagesPerFrame' into 'main'

Receive multiple messages per frame and fix some unhandled exceptions

See merge request !1
parents db854459 d430f285
No related branches found
No related tags found
1 merge request!1Receive multiple messages per frame and fix some unhandled exceptions
......@@ -17,7 +17,8 @@ namespace bessw.Unity.WebView.ChromeDevTools
private Process browserProcess;
/* browser settings */
public static string browserExecutablePath = Application.streamingAssetsPath + "/chrome-headless-shell-win64/chrome-headless-shell.exe";
public static string BrowserExecutablePath = "chrome";
public static string headlessBrowserExecutablePath = Application.streamingAssetsPath + "/chrome-headless-shell-win64/chrome-headless-shell.exe";
public static bool headless = true;
private const int debugPort = 9222;
......@@ -67,7 +68,14 @@ namespace bessw.Unity.WebView.ChromeDevTools
if (browserProcess == null || browserProcess.HasExited)
{
browserProcess = new Process();
browserProcess.StartInfo.FileName = browserExecutablePath;
if (headless)
{
browserProcess.StartInfo.FileName = headlessBrowserExecutablePath;
}
else
{
browserProcess.StartInfo.FileName = BrowserExecutablePath;
}
browserProcess.StartInfo.Arguments = String.Join(" ", new []{
$"--user-data-dir={Path.Join(Application.temporaryCachePath, "BrowserView")}",
$"--remote-debugging-port={debugPort}",
......
......@@ -77,9 +77,10 @@ namespace bessw.Unity.WebView.ChromeDevTools
_document = null;
}
public IEnumerator Update()
public void Update()
{
yield return devtools.readLoop();
//yield return devtools.readLoop();
devtools.handleMessages();
}
public async Task SetSize(Vector2Int size)
......
......@@ -21,6 +21,7 @@ namespace bessw.Unity.WebView.ChromeDevTools
private IDevtoolsConnection devtools;
private ConcurrentQueue<string> messageQueue = new();
private ConcurrentDictionary<long, ResponseTypeAndCallback<IDevtoolsResponse>> commandResponseDict = new();
// devtools events
......@@ -44,12 +45,27 @@ namespace bessw.Unity.WebView.ChromeDevTools
public DevtoolsProtocolHandler(IDevtoolsConnection devtools)
{
this.devtools = devtools;
devtools.receivedMessage += ParseMessage;
//TODO: let devtools open async
devtools.receivedMessage += enqueueMessage;
//TODO: let devtools open async (some other code currently expects it to be open once this method returns)
devtools.OpenAsync().Wait();
//devtools.AsyncReadLoop();
// run AsyncReadLoop in a c# background thread to be able to receive more then one message per Unity update() call
Task.Run(devtools.AsyncReadLoop);
}
private void enqueueMessage(string msg)
{
messageQueue.Enqueue(msg);
}
public void handleMessages()
{
while (messageQueue.TryDequeue(out string msgStr))
{
ParseMessage(msgStr);
}
}
[Obsolete]
public IEnumerator readLoop()
{
while (true)
......@@ -79,7 +95,15 @@ namespace bessw.Unity.WebView.ChromeDevTools
private void ParseMessage(string msgStr)
{
// deserialize the devtools response wrapper
var message = JObject.Parse(msgStr);
JObject message;
try
{
message = JObject.Parse(msgStr);
} catch (Exception e)
{
Debug.LogError($"Could not parse message '{msgStr}' error '{e.Message}'");
return;
}
if (message.ContainsKey("id"))
{
......
......@@ -38,7 +38,6 @@ namespace bessw.Unity.WebView.ChromeDevTools
/// Experimental! Synchronisation with Unity coroutines is not jet implemented. Call ReadAsync from a coroutine instead.
/// </summary>
/// <returns></returns>
[Obsolete]
public async Task AsyncReadLoop()
{
var cancellationToken = cancellationTokenSource.Token;
......
......@@ -23,7 +23,6 @@ namespace bessw.Unity.WebView.ChromeDevTools
/// experimental
/// </summary>
/// <returns></returns>
[Obsolete]
public Task AsyncReadLoop();
public Task SendCommandAsync(string command);
......
......@@ -922,12 +922,12 @@ namespace bessw.Unity.WebView.ChromeDevTools.Protocol.DOM
/// <summary>
/// Child nodes of this node when requested with children.
/// </summary>
public List<Node> children { get; set; }
public List<Node> children { get; set; } = new List<Node>();
/// <summary>
/// Attributes of the `Element` node in the form of flat array `[name1, value1, name2, value2]`.
/// </summary>
[JsonConverter(typeof(JsonInterleavedArrayConverter<string, string>))]
public Dictionary<string,string> attributes { get; set; }
public Dictionary<string,string> attributes { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Document URL that `Document` or `FrameOwner` node points to.
/// </summary>
......
......@@ -85,7 +85,7 @@ namespace bessw.Unity.WebView
//_ = tab.SetSize(Vector2Int.RoundToInt(this.gameObject.GetComponentInParent<Canvas>().renderingDisplaySize));
OnRectTransformDimensionsChange();
StartCoroutine(tab.Update());
//StartCoroutine(tab.Update());
//StartCoroutine(createScreenshots());
StartCoroutine(tab.StartStream(tab.Size.x, tab.Size.y, (frame) =>
{
......@@ -126,7 +126,7 @@ namespace bessw.Unity.WebView
// Update is called once per frame
private void Update()
{
tab?.Update();
}
#region pointer event handlers
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment