Skip to content

Commit 04aad3a

Browse files
committed
set websocket send buffer to 4096
1 parent 17493a0 commit 04aad3a

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Common/Utils/JsonWebSocketUtils.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.IO.Pipelines;
23
using System.Net.WebSockets;
34
using System.Text.Json;
45
using Microsoft.IO;
@@ -53,16 +54,16 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
5354
}
5455
}
5556

56-
public static Task SendFullMessage<T>(T obj, WebSocket socket, CancellationToken cancelToken, int maxChunkSize = 256) =>
57-
SendFullMessageBytes(JsonSerializer.SerializeToUtf8Bytes(obj), socket, cancelToken);
57+
public static Task SendFullMessage<T>(T obj, WebSocket socket, CancellationToken cancelToken, int bufferSize = 4096) =>
58+
SendFullMessageBytes(JsonSerializer.SerializeToUtf8Bytes(obj), socket, cancelToken, bufferSize);
5859

59-
public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, CancellationToken cancelToken, int maxChunkSize = 256)
60+
public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, CancellationToken cancelToken, int bufferSize)
6061
{
6162
var doneBytes = 0;
6263

6364
while (doneBytes < msg.Length)
6465
{
65-
var bytesProcessing = Math.Min(maxChunkSize, msg.Length - doneBytes);
66+
var bytesProcessing = Math.Min(bufferSize, msg.Length - doneBytes);
6667
var buffer = msg.AsMemory(doneBytes, bytesProcessing);
6768

6869
doneBytes += bytesProcessing;

Common/Websocket/WebsockBaseController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public abstract class WebsocketBaseController<T> : OpenShockControllerBase, IAsy
2222
/// <inheritdoc />
2323
public abstract Guid Id { get; }
2424

25-
public virtual int MaxChunkSize => 256;
26-
2725
/// <summary>
2826
/// Logger
2927
/// </summary>
@@ -182,7 +180,7 @@ private async Task MessageLoop()
182180
/// <returns></returns>
183181
[NonAction]
184182
protected virtual Task SendWebSocketMessage(T message, WebSocket websocket, CancellationToken cancellationToken) =>
185-
JsonWebSocketUtils.SendFullMessage(message, websocket, cancellationToken, MaxChunkSize);
183+
JsonWebSocketUtils.SendFullMessage(message, websocket, cancellationToken);
186184

187185

188186
#endregion

0 commit comments

Comments
 (0)