diff --git a/Runtime/ChromeDevtools/BrowserTab.cs b/Runtime/ChromeDevtools/BrowserTab.cs index f4f5ced289cde4923a2e3aace29f838dd9deb966..9d85da62ec2443a7ccab7e10f89355b38f8b8db8 100644 --- a/Runtime/ChromeDevtools/BrowserTab.cs +++ b/Runtime/ChromeDevtools/BrowserTab.cs @@ -9,6 +9,7 @@ using System.IO; using System.Net.WebSockets; using System.Text; using System.Threading; +using System.Threading.Tasks; using UnityEngine; namespace ChromeDevTools @@ -45,12 +46,13 @@ namespace ChromeDevTools /// <summary> + /// Coroutine wrapper for _SendWsMessagel� /// json serializes and sends a command over the devtools websocket /// </summary> /// <typeparam name="T">IDevtoolsCommand</typeparam> /// <param name="command"></param> /// <returns></returns> - private IEnumerator SendWsMessage<T>(T command, System.Action<IDevtoolsResponse> callback) where T: IDevtoolsCommand + private IEnumerator SendWsMessage<T>(T command, System.Action<IDevtoolsResponse> callback) where T : IDevtoolsCommand { // wait if the websocket is not yet open if (ws.State != WebSocketState.Open) @@ -58,6 +60,25 @@ namespace ChromeDevTools yield return new WaitUntil(() => ws.State == WebSocketState.Open); } + var sendTask = _SendWsMessage(command, callback); + // wait until the command has been send + yield return new WaitUntil(() => sendTask.IsCompleted); + } + + /// <summary> + /// json serializes and sends a command over the devtools websocket + /// </summary> + /// <typeparam name="T">IDevtoolsCommand</typeparam> + /// <param name="command"></param> + /// <returns>Task that resoves then the command is send</returns> + private async Task _SendWsMessage<T>(T command, System.Action<IDevtoolsResponse> callback) where T : IDevtoolsCommand + { + // wait if the websocket is not yet open + if (ws.State != WebSocketState.Open) + { + throw new InvalidOperationException("Websocket is not open"); + } + // apply the message wrapper var wrappedCommand = new DevtoolsCommandWrapper<T>(command); @@ -76,11 +97,8 @@ namespace ChromeDevTools // json serialize the command and send it var json = JsonConvert.SerializeObject(wrappedCommand, Browser.serializerSettings); - UnityEngine.Debug.Log($"ws send: '{json}'"); - var sendTask = ws.SendAsync(Encoding.UTF8.GetBytes(json), WebSocketMessageType.Text, true, Browser.cancellationTokenSource.Token); - - // wait until the command has been send - yield return new WaitUntil(() => sendTask.IsCompleted); + Debug.Log($"ws send: '{json}'"); + await ws.SendAsync(Encoding.UTF8.GetBytes(json), WebSocketMessageType.Text, true, Browser.cancellationTokenSource.Token); } /// <summary> @@ -180,15 +198,15 @@ namespace ChromeDevTools { Debug.Log($"BrowserTab close called for: '{pageTarget.Url}'"); //TODO: fix SendWsMessage without coroutine - SendWsMessage(new closeTarget(pageTarget.Id), delegate - { - ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "tab closed", CancellationToken.None) - .ContinueWith(delegate - { - Debug.Log($"ws CloseAsync State:\n{ws.State}"); - ws.Dispose(); - }); - }); + _ = _SendWsMessage(new closeTarget(pageTarget.Id), delegate + { + ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "tab closed", CancellationToken.None) + .ContinueWith(delegate + { + Debug.Log($"ws CloseAsync State:\n{ws.State}"); + ws.Dispose(); + }); + }); } } }