Skip to content

Commit 9b6e925

Browse files
committed
[websocket] support send ReadOnlySequence<byte>
1 parent a2d1fb1 commit 9b6e925

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/SuperSocket.WebSocket.Server/WebSocketSession.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data, CancellationToken
5959
cancellationToken);
6060
}
6161

62+
public virtual ValueTask SendAsync(ReadOnlySequence<byte> data, CancellationToken cancellationToken = default)
63+
{
64+
return SendAsync(new WebSocketPackage
65+
{
66+
OpCode = OpCode.Binary,
67+
Data = data,
68+
},
69+
cancellationToken);
70+
}
71+
6272
public ValueTask CloseAsync(CloseReason reason, string reasonText = null, CancellationToken cancellationToken = default)
6373
{
6474
var closeReasonCode = (short)reason;

test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,56 @@ public async Task TestTextMessageSendReceive(Type hostConfiguratorType, int conn
428428
}
429429
}
430430

431+
[Theory]
432+
[InlineData(typeof(RegularHostConfigurator))]
433+
[InlineData(typeof(SecureHostConfigurator))]
434+
public async Task TestSendReadOnlySequence(Type hostConfiguratorType)
435+
{
436+
var hostConfigurator = CreateObject<IHostConfigurator>(hostConfiguratorType);
437+
438+
using (var server = CreateWebSocketServerBuilder(builder =>
439+
{
440+
return builder.UseWebSocketMessageHandler(async (session, message) =>
441+
{
442+
await session.SendAsync(message.Data);
443+
});
444+
}, hostConfigurator).BuildAsServer())
445+
{
446+
Assert.True(await server.StartAsync());
447+
OutputHelper.WriteLine("Server started.");
448+
449+
var websocket = new ClientWebSocket();
450+
451+
websocket.Options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
452+
453+
var startConnectTime = DateTime.Now;
454+
await websocket.ConnectAsync(new Uri($"{hostConfigurator.WebSocketSchema}://localhost:{hostConfigurator.Listener.Port}"), CancellationToken.None);
455+
OutputHelper.WriteLine($"Took {DateTime.Now.Subtract(startConnectTime)} to establish the connection from client side.");
456+
457+
Assert.Equal(WebSocketState.Open, websocket.State);
458+
459+
var receiveBuffer = new byte[256];
460+
461+
for (var i = 0; i < 100; i++)
462+
{
463+
var message = Guid.NewGuid().ToString();
464+
var data = _encoding.GetBytes(message);
465+
var segment = new ArraySegment<byte>(data, 0, data.Length);
466+
467+
await websocket.SendAsync(new ArraySegment<byte>(data, 0, data.Length), WebSocketMessageType.Binary, true, CancellationToken.None);
468+
var receivedMessage = await GetWebSocketReply(websocket, receiveBuffer, WebSocketMessageType.Binary);
469+
470+
Assert.Equal(message, receivedMessage);
471+
}
472+
473+
await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
474+
475+
Assert.Equal(WebSocketState.Closed, websocket.State);
476+
477+
await server.StopAsync();
478+
}
479+
}
480+
431481
[Theory]
432482
[InlineData(typeof(RegularHostConfigurator), 10)]
433483
[InlineData(typeof(SecureHostConfigurator), 10)]

0 commit comments

Comments
 (0)