コード例 #1
0
        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);
                    }
                }
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        public async Task HandleAsync(
            IWebSocketContext context,
            GenericOperationMessage message,
            CancellationToken cancellationToken)
        {
            await context.SendConnectionAcceptMessageAsync(
                cancellationToken);

            await context.SendConnectionKeepAliveMessageAsync(
                cancellationToken);
        }
コード例 #4
0
        private async Task KeepConnectionAliveAsync()
        {
            while (!_context.Closed || !_cts.IsCancellationRequested)
            {
                await Task.Delay(_timeout, _cts.Token)
                .ConfigureAwait(false);

                await _context
                .SendConnectionKeepAliveMessageAsync(_cts.Token)
                .ConfigureAwait(false);
            }
        }
コード例 #5
0
 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);
         }
     }
 }