示例#1
0
        public async Task ProcessRequests(ConnectionContext connection)
        {
            var formatter = _formatterResolver.GetFormatter <WeatherReport>(
                (string)connection.Features.Get <IConnectionMetadataFeature>().Metadata["format"]);

            while (true)
            {
                var result = await connection.Transport.Input.ReadAsync();

                var buffer = result.Buffer;
                try
                {
                    if (!buffer.IsEmpty)
                    {
                        var stream = new MemoryStream();
                        var data   = buffer.ToArray();
                        await stream.WriteAsync(data, 0, data.Length);

                        stream.Position = 0;
                        var weatherReport = await formatter.ReadAsync(stream);

                        await _lifetimeManager.SendToAllAsync(weatherReport);
                    }
                    else if (result.IsCompleted)
                    {
                        break;
                    }
                }
                finally
                {
                    connection.Transport.Input.AdvanceTo(buffer.End);
                }
            }
        }
示例#2
0
        public async Task ProcessRequests(ConnectionContext connection)
        {
            var formatter = _formatterResolver.GetFormatter <WeatherReport>(
                (string)connection.Metadata["formatType"]);

            while (await connection.Transport.In.WaitToReadAsync())
            {
                if (connection.Transport.In.TryRead(out var buffer))
                {
                    var stream = new MemoryStream();
                    await stream.WriteAsync(buffer, 0, buffer.Length);

                    stream.Position = 0;
                    var weatherReport = await formatter.ReadAsync(stream);

                    await _lifetimeManager.SendToAllAsync(weatherReport);
                }
            }
        }