Skip to content
This repository was archived by the owner on Feb 20, 2022. It is now read-only.

Commit 59058be

Browse files
committed
use buffer when receiving data > than 1024 byte (use 10 MB as max. size)
1 parent 0e41dab commit 59058be

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

unity-project/Assets/Moulin/DDP/websocket/WebSocketSystemNet.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,30 @@ public override async Task ConnectAsync()
6767
await Task.Factory.StartNew(
6868
async () =>
6969
{
70-
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
7170
if (webSocket.State == WebSocketState.Open)
7271
{
7372
OnOpen?.Invoke();
7473
}
7574
try
7675
{
76+
//Lets assume messages do not get bigger than 10 MB
77+
int maxSize = 10 * 1024 * 1024;
78+
byte[] buffer = new byte[maxSize];
7779
while (true)
7880
{
79-
WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer, cts.Token);
81+
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), cts.Token);
8082
if (webSocket.State != WebSocketState.Open)
8183
{
8284
break;
8385
}
86+
if (result.Count >= maxSize || !result.EndOfMessage)
87+
{
88+
OnError?.Invoke("Maximum size for message exceeded");
89+
// the next message will also fail because we will send incorrect JSON to the encoder.
90+
continue;
91+
}
8492

85-
string json = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
93+
string json = Encoding.UTF8.GetString(buffer, 0, result.Count);
8694
OnMessage?.Invoke(json);
8795
}
8896
OnClose?.Invoke(true);

0 commit comments

Comments
 (0)