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

read the whole websoket message in one async call

parent ed530a89
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,7 @@ namespace ChromeDevTools
// open remote devtools websocket connection
/**
* Note: Forebg wl compatibility you need to use the JavaScript plugin, see also https://docs.unity3d.com/Manual/webgl-networking.html#UsingWebSockets
* Note: For webgl compatibility you need to use the JavaScript plugin, see also https://docs.unity3d.com/Manual/webgl-networking.html#UsingWebSockets
*/
ws.ConnectAsync(new Uri(pageTarget.WebSocketDebuggerUrl), Browser.cancellationTokenSource.Token);
}
......@@ -116,8 +116,8 @@ namespace ChromeDevTools
// start the message receive loop (it will exit when the coroutine is stopped)
while (true)
{
string msgString;
async Task<string> read()
{
// create a MemoryStream to reconstruct the message
using (var ms = new MemoryStream())
{
......@@ -129,28 +129,33 @@ namespace ChromeDevTools
do
{
var messageBuffer = WebSocket.CreateClientBuffer(1024, 16);
var reseiveTask = ws.ReceiveAsync(messageBuffer, Browser.cancellationTokenSource.Token);
// yield the coroutine until the async Task "reseiveTask" is completed
yield return new WaitUntil(() => reseiveTask.IsCompleted);
result = reseiveTask.Result;
result = await ws.ReceiveAsync(messageBuffer, Browser.cancellationTokenSource.Token);
// write the messageBuffer to the MemoryStream
ms.Write(messageBuffer.Array, messageBuffer.Offset, result.Count);
}
while (!result.EndOfMessage);
} while (!result.EndOfMessage);
// If the webSocket message type isn't text ignore this message
if (!(result.MessageType == WebSocketMessageType.Text))
{
Debug.LogError($"Unexpected WebSocketMessageType: {result.MessageType}");
continue;
return await read();
}
// convert the message stream to string
msgString = Encoding.UTF8.GetString(ms.ToArray());
Debug.Log($"ws reseived: '{msgString}'");
return Encoding.UTF8.GetString(ms.ToArray());
}
}
var readTask = read();
// yield the coroutine until the async Task "reseiveTask" is completed
yield return new WaitUntil(() => readTask.IsCompleted);
string msgString = readTask.Result;
Debug.Log($"ws reseived: '{msgString}'");
// deserialize the devtools response wrapper
var response = JsonConvert.DeserializeObject<DevtoolsResponseWrapper>(msgString, Browser.serializerSettings);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment