예제 #1
0
        private async Task <(byte[] data, bool isText)> ReceiveMessageAsync(CancellationToken ct = default)
        {
            var buffer = new ArraySegment <byte>(new byte[4096]);
            var ms     = new SwappingStream();

            WebSocketReceiveResult result;

            do
            {
                ct.ThrowIfCancellationRequested();
                result = await WebSocket.ReceiveAsync(buffer, ct).ConfigureAwait(false);

                if (buffer.Array != null)
                {
                    await ms.WriteAsync(buffer.Array, buffer.Offset, result.Count, ct).ConfigureAwait(false);
                }
            } while (!result.EndOfMessage);

            ms.Seek(0, SeekOrigin.Begin);

            return(
                data : await ms.GetBytesAsync().ConfigureAwait(false),
                isText : result.MessageType == WebSocketMessageType.Binary
                );
        }
예제 #2
0
        /// <inheritdoc />
        public async IAsyncEnumerable <T> Populate <T>(IAsyncEnumerable <T> entities, byte[] body)
        {
            await using var jsonStream = new SwappingStream();
            var populateStream = new MemoryStream(body);

#if NETSTANDARD2_1
            await using (populateStream)
#else
            using (populateStream)
#endif
            {
                await ProduceJsonArray(populateStream, jsonStream).ConfigureAwait(false);

                await foreach (var item in JsonProvider.Populate(entities, await jsonStream.GetBytesAsync()).ConfigureAwait(false))
                {
                    yield return(item);
                }
            }
        }