C
C#13mo ago
Pat

❔ Reading from ClientWebSocket with BinaryReader

I am building an app to work as a gateway between a consumer connecting to a .NET websocket server and then proxying that to a TCP backend server. This code currently works quite well for TCP clients and makes use of the BinaryReader / BinaryWriter to proxy between the WebSocket and the TCPClient classes.
I'm trying to replicate the same behavior between a ClientWebSocket and a Websocket but can't seem to find a solution to reading each "packet" the binary ClientWebSocket receives. Wireshark shows the data I would expect but calling ClientWebSocket.ReceiveAsync results in odd counts that do not match the packets.
2 Replies
Pat
Pat13mo ago
An example of proxying this traffic with the TCP client:
NetworkStream stream = myBinaryReader.BaseStream;
do
{
if (!stream.DataAvailable)
{
continue;
}

var bytesRead = await stream.ReadAsync(buffer, ct);
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, read), WebSocketMessageType.Binary, true, ct)
} while( --things are connected-- )
NetworkStream stream = myBinaryReader.BaseStream;
do
{
if (!stream.DataAvailable)
{
continue;
}

var bytesRead = await stream.ReadAsync(buffer, ct);
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, read), WebSocketMessageType.Binary, true, ct)
} while( --things are connected-- )
As mentioned above, this works very well. Is it possible with ClientWebSocket to access a similar stream?
Accord
Accord13mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.