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 merge requests found
...@@ -39,7 +39,7 @@ namespace ChromeDevTools ...@@ -39,7 +39,7 @@ namespace ChromeDevTools
// open remote devtools websocket connection // 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); ws.ConnectAsync(new Uri(pageTarget.WebSocketDebuggerUrl), Browser.cancellationTokenSource.Token);
} }
...@@ -116,8 +116,8 @@ namespace ChromeDevTools ...@@ -116,8 +116,8 @@ namespace ChromeDevTools
// start the message receive loop (it will exit when the coroutine is stopped) // start the message receive loop (it will exit when the coroutine is stopped)
while (true) while (true)
{ {
string msgString; async Task<string> read()
{
// create a MemoryStream to reconstruct the message // create a MemoryStream to reconstruct the message
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {
...@@ -129,28 +129,33 @@ namespace ChromeDevTools ...@@ -129,28 +129,33 @@ namespace ChromeDevTools
do do
{ {
var messageBuffer = WebSocket.CreateClientBuffer(1024, 16); var messageBuffer = WebSocket.CreateClientBuffer(1024, 16);
var reseiveTask = ws.ReceiveAsync(messageBuffer, Browser.cancellationTokenSource.Token); result = await 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;
// write the messageBuffer to the MemoryStream // write the messageBuffer to the MemoryStream
ms.Write(messageBuffer.Array, messageBuffer.Offset, result.Count); 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 the webSocket message type isn't text ignore this message
if (!(result.MessageType == WebSocketMessageType.Text)) if (!(result.MessageType == WebSocketMessageType.Text))
{ {
Debug.LogError($"Unexpected WebSocketMessageType: {result.MessageType}"); Debug.LogError($"Unexpected WebSocketMessageType: {result.MessageType}");
continue; return await read();
} }
// convert the message stream to string // convert the message stream to string
msgString = Encoding.UTF8.GetString(ms.ToArray()); return Encoding.UTF8.GetString(ms.ToArray());
Debug.Log($"ws reseived: '{msgString}'");
} }
}
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 // deserialize the devtools response wrapper
var response = JsonConvert.DeserializeObject<DevtoolsResponseWrapper>(msgString, Browser.serializerSettings); 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