private async Task ReceiveMessagesAsync( CancellationToken cancellationToken) { using (var combined = CancellationTokenSource .CreateLinkedTokenSource(cancellationToken, _cts.Token)) { while (!_context.CloseStatus.HasValue || !combined.IsCancellationRequested) { GenericOperationMessage message = await _context .ReceiveMessageAsync(combined.Token); if (message == null) { await _context.SendConnectionKeepAliveMessageAsync( combined.Token).ConfigureAwait(false); } else { await HandleMessage(message, combined.Token) .ConfigureAwait(false); } } } }
public async Task StartAsync(CancellationToken cancellationToken) { using (var combined = CancellationTokenSource .CreateLinkedTokenSource(cancellationToken, _cts.Token)) { while (!_webSocket.Closed || !combined.IsCancellationRequested) { await _webSocket .ReceiveMessageAsync(_writer, combined.Token) .ConfigureAwait(false); await WriteMessageDelimiterAsync(combined.Token) .ConfigureAwait(false); } _writer.Complete(); } }
public static async Task <GenericOperationMessage> ReceiveMessageAsync( this IWebSocketContext context, CancellationToken cancellationToken) { using (var messageStream = new MemoryStream()) { await context.ReceiveMessageAsync( messageStream, cancellationToken); string json = Encoding.UTF8.GetString(messageStream.ToArray()); if (string.IsNullOrEmpty(json?.Trim())) { return(null); } return(JsonConvert .DeserializeObject <GenericOperationMessage>(json)); } }