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 HandleAsync( IWebSocketContext context, GenericOperationMessage message, CancellationToken cancellationToken) { ConnectionStatus connectionStatus = await context.OpenAsync(message.Payload.ToDictionary()) .ConfigureAwait(false); if (connectionStatus.Accepted) { await context.SendConnectionAcceptMessageAsync( cancellationToken).ConfigureAwait(false); await context.SendConnectionKeepAliveMessageAsync( cancellationToken).ConfigureAwait(false); } else { await context.SendConnectionErrorMessageAsync( connectionStatus.Response, cancellationToken) .ConfigureAwait(false); await context.CloseAsync().ConfigureAwait(false); } }
public async Task HandleAsync( IWebSocketContext context, GenericOperationMessage message, CancellationToken cancellationToken) { await context.SendConnectionAcceptMessageAsync( cancellationToken); await context.SendConnectionKeepAliveMessageAsync( cancellationToken); }
private async Task KeepConnectionAliveAsync() { while (!_context.Closed || !_cts.IsCancellationRequested) { await Task.Delay(_timeout, _cts.Token) .ConfigureAwait(false); await _context .SendConnectionKeepAliveMessageAsync(_cts.Token) .ConfigureAwait(false); } }
internal async Task ProcessMessageAsync( ReadOnlySequence <byte> slice, CancellationToken cancellationToken) { using (var combined = CancellationTokenSource .CreateLinkedTokenSource(cancellationToken, _cts.Token)) { string json = Encoding.UTF8.GetString(slice.ToArray()); if (!string.IsNullOrEmpty(json.Trim())) { await HandleMessageAsync(json, combined.Token) .ConfigureAwait(false); } else { await _context .SendConnectionKeepAliveMessageAsync(combined.Token) .ConfigureAwait(false); } } }