I am trying to use Proto-buf c# library in .net. I am using an ApiController and the System.Net.WebSockets namespace. It has a way of getting an incoming message.
ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
I need a stream to use protobuf. I am not sure how to get one with this namespace since it is using an ArraySegment.
I was able to send a proto-buf object to decode/protobuf.js, but now have constructed an object on the js client and am sending to the server.
Example cust = Serializer.DeserializeWithLengthPrefix(result, PrefixStyle.Base128);
If you just need a Stream
to deserialize from, you can use:
var stream = new MemoryStream(buffer.Array, buffer.Offset, result.Count);
ArraySegment<byte>
is already wrapping a byte[]
, so you just need to create a MemoryStream
wrapping the same byte array, with the right offset and length.
See more on this question at Stackoverflow