Skip to content
Snippets Groups Projects
Verified Commit 52ace640 authored by Björn Eßwein's avatar Björn Eßwein
Browse files

made sendWsMessage callable without coroutine

parent 9b78d579
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ using System.IO; ...@@ -9,6 +9,7 @@ using System.IO;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
namespace ChromeDevTools namespace ChromeDevTools
...@@ -45,6 +46,7 @@ namespace ChromeDevTools ...@@ -45,6 +46,7 @@ namespace ChromeDevTools
/// <summary> /// <summary>
/// Coroutine wrapper for _SendWsMessagel
/// json serializes and sends a command over the devtools websocket /// json serializes and sends a command over the devtools websocket
/// </summary> /// </summary>
/// <typeparam name="T">IDevtoolsCommand</typeparam> /// <typeparam name="T">IDevtoolsCommand</typeparam>
...@@ -58,6 +60,25 @@ namespace ChromeDevTools ...@@ -58,6 +60,25 @@ namespace ChromeDevTools
yield return new WaitUntil(() => ws.State == WebSocketState.Open); 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 // apply the message wrapper
var wrappedCommand = new DevtoolsCommandWrapper<T>(command); var wrappedCommand = new DevtoolsCommandWrapper<T>(command);
...@@ -76,11 +97,8 @@ namespace ChromeDevTools ...@@ -76,11 +97,8 @@ namespace ChromeDevTools
// json serialize the command and send it // json serialize the command and send it
var json = JsonConvert.SerializeObject(wrappedCommand, Browser.serializerSettings); var json = JsonConvert.SerializeObject(wrappedCommand, Browser.serializerSettings);
UnityEngine.Debug.Log($"ws send: '{json}'"); Debug.Log($"ws send: '{json}'");
var sendTask = ws.SendAsync(Encoding.UTF8.GetBytes(json), WebSocketMessageType.Text, true, Browser.cancellationTokenSource.Token); await 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);
} }
/// <summary> /// <summary>
...@@ -180,7 +198,7 @@ namespace ChromeDevTools ...@@ -180,7 +198,7 @@ namespace ChromeDevTools
{ {
Debug.Log($"BrowserTab close called for: '{pageTarget.Url}'"); Debug.Log($"BrowserTab close called for: '{pageTarget.Url}'");
//TODO: fix SendWsMessage without coroutine //TODO: fix SendWsMessage without coroutine
SendWsMessage(new closeTarget(pageTarget.Id), delegate _ = _SendWsMessage(new closeTarget(pageTarget.Id), delegate
{ {
ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "tab closed", CancellationToken.None) ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "tab closed", CancellationToken.None)
.ContinueWith(delegate .ContinueWith(delegate
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment