Skip to content
Snippets Groups Projects
Commit d430f285 authored by Björn Eßwein's avatar Björn Eßwein Committed by Silas Kuder
Browse files

Receive multiple messages per frame and fix some unhandled exceptions

parent db854459
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 ...@@ -17,7 +17,8 @@ namespace bessw.Unity.WebView.ChromeDevTools
private Process browserProcess; private Process browserProcess;
/* browser settings */ /* 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; public static bool headless = true;
private const int debugPort = 9222; private const int debugPort = 9222;
...@@ -67,7 +68,14 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -67,7 +68,14 @@ namespace bessw.Unity.WebView.ChromeDevTools
if (browserProcess == null || browserProcess.HasExited) if (browserProcess == null || browserProcess.HasExited)
{ {
browserProcess = new Process(); browserProcess = new Process();
browserProcess.StartInfo.FileName = browserExecutablePath; if (headless)
{
browserProcess.StartInfo.FileName = headlessBrowserExecutablePath;
}
else
{
browserProcess.StartInfo.FileName = BrowserExecutablePath;
}
browserProcess.StartInfo.Arguments = String.Join(" ", new []{ browserProcess.StartInfo.Arguments = String.Join(" ", new []{
$"--user-data-dir={Path.Join(Application.temporaryCachePath, "BrowserView")}", $"--user-data-dir={Path.Join(Application.temporaryCachePath, "BrowserView")}",
$"--remote-debugging-port={debugPort}", $"--remote-debugging-port={debugPort}",
......
...@@ -77,9 +77,10 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -77,9 +77,10 @@ namespace bessw.Unity.WebView.ChromeDevTools
_document = null; _document = null;
} }
public IEnumerator Update() public void Update()
{ {
yield return devtools.readLoop(); //yield return devtools.readLoop();
devtools.handleMessages();
} }
public async Task SetSize(Vector2Int size) public async Task SetSize(Vector2Int size)
......
...@@ -21,6 +21,7 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -21,6 +21,7 @@ namespace bessw.Unity.WebView.ChromeDevTools
private IDevtoolsConnection devtools; private IDevtoolsConnection devtools;
private ConcurrentQueue<string> messageQueue = new();
private ConcurrentDictionary<long, ResponseTypeAndCallback<IDevtoolsResponse>> commandResponseDict = new(); private ConcurrentDictionary<long, ResponseTypeAndCallback<IDevtoolsResponse>> commandResponseDict = new();
// devtools events // devtools events
...@@ -44,12 +45,27 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -44,12 +45,27 @@ namespace bessw.Unity.WebView.ChromeDevTools
public DevtoolsProtocolHandler(IDevtoolsConnection devtools) public DevtoolsProtocolHandler(IDevtoolsConnection devtools)
{ {
this.devtools = devtools; this.devtools = devtools;
devtools.receivedMessage += ParseMessage; devtools.receivedMessage += enqueueMessage;
//TODO: let devtools open async //TODO: let devtools open async (some other code currently expects it to be open once this method returns)
devtools.OpenAsync().Wait(); 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() public IEnumerator readLoop()
{ {
while (true) while (true)
...@@ -79,7 +95,15 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -79,7 +95,15 @@ namespace bessw.Unity.WebView.ChromeDevTools
private void ParseMessage(string msgStr) private void ParseMessage(string msgStr)
{ {
// deserialize the devtools response wrapper // 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")) if (message.ContainsKey("id"))
{ {
......
...@@ -38,7 +38,6 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -38,7 +38,6 @@ namespace bessw.Unity.WebView.ChromeDevTools
/// Experimental! Synchronisation with Unity coroutines is not jet implemented. Call ReadAsync from a coroutine instead. /// Experimental! Synchronisation with Unity coroutines is not jet implemented. Call ReadAsync from a coroutine instead.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Obsolete]
public async Task AsyncReadLoop() public async Task AsyncReadLoop()
{ {
var cancellationToken = cancellationTokenSource.Token; var cancellationToken = cancellationTokenSource.Token;
......
...@@ -23,7 +23,6 @@ namespace bessw.Unity.WebView.ChromeDevTools ...@@ -23,7 +23,6 @@ namespace bessw.Unity.WebView.ChromeDevTools
/// experimental /// experimental
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[Obsolete]
public Task AsyncReadLoop(); public Task AsyncReadLoop();
public Task SendCommandAsync(string command); public Task SendCommandAsync(string command);
......
...@@ -922,12 +922,12 @@ namespace bessw.Unity.WebView.ChromeDevTools.Protocol.DOM ...@@ -922,12 +922,12 @@ namespace bessw.Unity.WebView.ChromeDevTools.Protocol.DOM
/// <summary> /// <summary>
/// Child nodes of this node when requested with children. /// Child nodes of this node when requested with children.
/// </summary> /// </summary>
public List<Node> children { get; set; } public List<Node> children { get; set; } = new List<Node>();
/// <summary> /// <summary>
/// Attributes of the `Element` node in the form of flat array `[name1, value1, name2, value2]`. /// Attributes of the `Element` node in the form of flat array `[name1, value1, name2, value2]`.
/// </summary> /// </summary>
[JsonConverter(typeof(JsonInterleavedArrayConverter<string, string>))] [JsonConverter(typeof(JsonInterleavedArrayConverter<string, string>))]
public Dictionary<string,string> attributes { get; set; } public Dictionary<string,string> attributes { get; set; } = new Dictionary<string, string>();
/// <summary> /// <summary>
/// Document URL that `Document` or `FrameOwner` node points to. /// Document URL that `Document` or `FrameOwner` node points to.
/// </summary> /// </summary>
......
...@@ -85,7 +85,7 @@ namespace bessw.Unity.WebView ...@@ -85,7 +85,7 @@ namespace bessw.Unity.WebView
//_ = tab.SetSize(Vector2Int.RoundToInt(this.gameObject.GetComponentInParent<Canvas>().renderingDisplaySize)); //_ = tab.SetSize(Vector2Int.RoundToInt(this.gameObject.GetComponentInParent<Canvas>().renderingDisplaySize));
OnRectTransformDimensionsChange(); OnRectTransformDimensionsChange();
StartCoroutine(tab.Update()); //StartCoroutine(tab.Update());
//StartCoroutine(createScreenshots()); //StartCoroutine(createScreenshots());
StartCoroutine(tab.StartStream(tab.Size.x, tab.Size.y, (frame) => StartCoroutine(tab.StartStream(tab.Size.x, tab.Size.y, (frame) =>
{ {
...@@ -126,7 +126,7 @@ namespace bessw.Unity.WebView ...@@ -126,7 +126,7 @@ namespace bessw.Unity.WebView
// Update is called once per frame // Update is called once per frame
private void Update() private void Update()
{ {
tab?.Update();
} }
#region pointer event handlers #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