diff --git a/lib/PuppeteerSharp.Tests/PollerInterceptor.cs b/lib/PuppeteerSharp.Tests/PollerInterceptor.cs index 3b4e54a81..6682d2ec5 100644 --- a/lib/PuppeteerSharp.Tests/PollerInterceptor.cs +++ b/lib/PuppeteerSharp.Tests/PollerInterceptor.cs @@ -6,9 +6,9 @@ namespace PuppeteerSharp.Tests { public sealed class PollerInterceptor(IConnectionTransport connectionTransport) : IConnectionTransport { - public event EventHandler MessageSent; + public event EventHandler MessageSent; - public Task SendAsync(string message) + public Task SendAsync(byte[] message) { var task = connectionTransport.SendAsync(message); MessageSent?.Invoke(connectionTransport, message); @@ -41,7 +41,7 @@ public Task WaitForStartPollingAsync() // We intercept the poller.start() call to prevent tests from continuing before the polling has started. MessageSent += (_, message) => { - if (message.Contains("poller.start()")) + if (message.AsSpan().IndexOf("poller.start()"u8) != -1) { startedPolling.SetResult(true); } diff --git a/lib/PuppeteerSharp/Cdp/Connection.cs b/lib/PuppeteerSharp/Cdp/Connection.cs index bec40fe67..a2f8d061b 100644 --- a/lib/PuppeteerSharp/Cdp/Connection.cs +++ b/lib/PuppeteerSharp/Cdp/Connection.cs @@ -157,14 +157,18 @@ internal static async Task Create(string url, IConnectionOptions con internal int GetMessageId() => Interlocked.Increment(ref _lastId); - internal Task RawSendAsync(string message, CommandOptions options = null) + internal Task RawSendAsync(byte[] message, CommandOptions options = null) { - _logger.LogTrace("Send ► {Message}", message); + if (_logger.IsEnabled(LogLevel.Trace)) + { + _logger.LogTrace("Send ► {Message}", Encoding.UTF8.GetString(message)); + } + return Transport.SendAsync(message); } - internal string GetMessage(int id, string method, object args, string sessionId = null) - => JsonSerializer.Serialize( + internal byte[] GetMessage(int id, string method, object args, string sessionId = null) + => JsonSerializer.SerializeToUtf8Bytes( new ConnectionRequest { Id = id, Method = method, Params = args, SessionId = sessionId }, JsonHelper.DefaultJsonSerializerSettings.Value); diff --git a/lib/PuppeteerSharp/MessageTask.cs b/lib/PuppeteerSharp/MessageTask.cs index c2166d9c3..1124eaff5 100644 --- a/lib/PuppeteerSharp/MessageTask.cs +++ b/lib/PuppeteerSharp/MessageTask.cs @@ -5,7 +5,7 @@ namespace PuppeteerSharp { internal class MessageTask { - internal string Message { get; set; } + internal byte[] Message { get; set; } internal TaskCompletionSource TaskWrapper { get; set; } diff --git a/lib/PuppeteerSharp/Transport/IConnectionTransport.cs b/lib/PuppeteerSharp/Transport/IConnectionTransport.cs index 48ccc7923..be2c5da5e 100644 --- a/lib/PuppeteerSharp/Transport/IConnectionTransport.cs +++ b/lib/PuppeteerSharp/Transport/IConnectionTransport.cs @@ -33,6 +33,6 @@ public interface IConnectionTransport : IDisposable /// /// Message to send. /// The task. - Task SendAsync(string message); + Task SendAsync(byte[] message); } } diff --git a/lib/PuppeteerSharp/Transport/WebSocketTransport.cs b/lib/PuppeteerSharp/Transport/WebSocketTransport.cs index d66506495..ea23d64dc 100644 --- a/lib/PuppeteerSharp/Transport/WebSocketTransport.cs +++ b/lib/PuppeteerSharp/Transport/WebSocketTransport.cs @@ -70,11 +70,9 @@ private WebSocketTransport(WebSocket client, TransportTaskScheduler scheduler, b public bool IsClosed { get; private set; } /// - public Task SendAsync(string message) + public Task SendAsync(byte[] message) { - var encoded = Encoding.UTF8.GetBytes(message); - var buffer = new ArraySegment(encoded, 0, encoded.Length); - Task SendCoreAsync() => _client.SendAsync(buffer, WebSocketMessageType.Text, true, default); + Task SendCoreAsync() => _client.SendAsync(new ArraySegment(message), WebSocketMessageType.Text, true, default); return _queueRequests ? _socketQueue.Enqueue(SendCoreAsync) : SendCoreAsync(); }