From 9b6e925a277af742845d783c78bddcab7b7a67f8 Mon Sep 17 00:00:00 2001 From: Kerry Jiang Date: Sat, 22 Jun 2024 09:10:35 -0700 Subject: [PATCH] [websocket] support send ReadOnlySequence --- .../WebSocketSession.cs | 10 ++++ .../WebSocket/WebSocketBasicTest.cs | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/SuperSocket.WebSocket.Server/WebSocketSession.cs b/src/SuperSocket.WebSocket.Server/WebSocketSession.cs index d37dd7f9e..21a741796 100644 --- a/src/SuperSocket.WebSocket.Server/WebSocketSession.cs +++ b/src/SuperSocket.WebSocket.Server/WebSocketSession.cs @@ -59,6 +59,16 @@ public virtual ValueTask SendAsync(ReadOnlyMemory data, CancellationToken cancellationToken); } + public virtual ValueTask SendAsync(ReadOnlySequence data, CancellationToken cancellationToken = default) + { + return SendAsync(new WebSocketPackage + { + OpCode = OpCode.Binary, + Data = data, + }, + cancellationToken); + } + public ValueTask CloseAsync(CloseReason reason, string reasonText = null, CancellationToken cancellationToken = default) { var closeReasonCode = (short)reason; diff --git a/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs b/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs index b12ac87ae..90b388380 100644 --- a/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs +++ b/test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs @@ -428,6 +428,56 @@ public async Task TestTextMessageSendReceive(Type hostConfiguratorType, int conn } } + [Theory] + [InlineData(typeof(RegularHostConfigurator))] + [InlineData(typeof(SecureHostConfigurator))] + public async Task TestSendReadOnlySequence(Type hostConfiguratorType) + { + var hostConfigurator = CreateObject(hostConfiguratorType); + + using (var server = CreateWebSocketServerBuilder(builder => + { + return builder.UseWebSocketMessageHandler(async (session, message) => + { + await session.SendAsync(message.Data); + }); + }, hostConfigurator).BuildAsServer()) + { + Assert.True(await server.StartAsync()); + OutputHelper.WriteLine("Server started."); + + var websocket = new ClientWebSocket(); + + websocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + + var startConnectTime = DateTime.Now; + await websocket.ConnectAsync(new Uri($"{hostConfigurator.WebSocketSchema}://localhost:{hostConfigurator.Listener.Port}"), CancellationToken.None); + OutputHelper.WriteLine($"Took {DateTime.Now.Subtract(startConnectTime)} to establish the connection from client side."); + + Assert.Equal(WebSocketState.Open, websocket.State); + + var receiveBuffer = new byte[256]; + + for (var i = 0; i < 100; i++) + { + var message = Guid.NewGuid().ToString(); + var data = _encoding.GetBytes(message); + var segment = new ArraySegment(data, 0, data.Length); + + await websocket.SendAsync(new ArraySegment(data, 0, data.Length), WebSocketMessageType.Binary, true, CancellationToken.None); + var receivedMessage = await GetWebSocketReply(websocket, receiveBuffer, WebSocketMessageType.Binary); + + Assert.Equal(message, receivedMessage); + } + + await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); + + Assert.Equal(WebSocketState.Closed, websocket.State); + + await server.StopAsync(); + } + } + [Theory] [InlineData(typeof(RegularHostConfigurator), 10)] [InlineData(typeof(SecureHostConfigurator), 10)]