Skip to content

Commit

Permalink
[websocket] support send ReadOnlySequence<byte>
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Jun 22, 2024
1 parent a2d1fb1 commit 9b6e925
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/SuperSocket.WebSocket.Server/WebSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data, CancellationToken
cancellationToken);
}

public virtual ValueTask SendAsync(ReadOnlySequence<byte> 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;
Expand Down
50 changes: 50 additions & 0 deletions test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHostConfigurator>(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<byte>(data, 0, data.Length);

await websocket.SendAsync(new ArraySegment<byte>(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)]
Expand Down

0 comments on commit 9b6e925

Please sign in to comment.