C
C#2y ago
occluder

❔ Confusing ClientWebSocket.ReceiveAsync() behavior

Picture the following class:
private ClientWebSocket _ws = new();
private Memory<byte> _buffer = new(new byte[4096]);

public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
ValueWebSocketReceiveResult received = await _ws.ReceiveAsync(_buffer, CancellationToken.None);
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
private ClientWebSocket _ws = new();
private Memory<byte> _buffer = new(new byte[4096]);

public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
ValueWebSocketReceiveResult received = await _ws.ReceiveAsync(_buffer, CancellationToken.None);
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
In this scenario, one would expect that the client can receive up to 4kb of data. However, the behavior I'm encountering is that it's only using the first 2048 bytes of the buffer. With a bit of googling I found out that I'm supposed to use the boolean ValueWebSocketReceiveResult.EndOfMessage to check if the data was split into multiple messages. This made me a little confused, why would it split the data into multiple messages if my buffer is big enough? No matter, I changed my code to the following, hoping I could get around this issue without creating a new buffer:
public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
int bufferRegion = 0;
ValueWebSocketReceiveResult received;
while (!(received = await _ws.ReceiveAsync(_buffer[bufferRegion * 2048], CancellationToken.None)).EndOfMessage)
{
bufferRegion++;
continue;
}
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
int bufferRegion = 0;
ValueWebSocketReceiveResult received;
while (!(received = await _ws.ReceiveAsync(_buffer[bufferRegion * 2048], CancellationToken.None)).EndOfMessage)
{
bufferRegion++;
continue;
}
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
But my efforts were in vain, as each time the inner while loop iterates it overrides the previous result, instead of appending to it. So looking back at all of it, something definitely feels off. Am I using the buffer wrong? Am I supposed to handle split messages differently? Or what's going on here?
2 Replies
occluder
occluderOP2y ago
I forgot to mention that I also tried upping the buffer size to 10kb at one point, but the behavior hasn't changed. I still was only getting 2048 bytes per message
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server