internal Task SendAsync(IEnumerable <IServerSentEventsClient> clients, ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
        {
            List <Task> clientsTasks = null;

            foreach (ServerSentEventsClient client in clients)
            {
                if (client.IsConnected)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    Task operationTask = client.SendAsync(serverSentEventBytes, cancellationToken);

                    if (operationTask.Status != TaskStatus.RanToCompletion)
                    {
                        if (clientsTasks is null)
                        {
                            clientsTasks = new List <Task>();
                        }

                        clientsTasks.Add(operationTask);
                    }
                }
            }

            return((clientsTasks is null) ? Task.CompletedTask : Task.WhenAll(clientsTasks));
        }
        /// <summary>
        /// Changes the interval after which clients will attempt to reestablish failed connections.
        /// </summary>
        /// <param name="reconnectInterval">The reconnect interval.</param>
        /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task ChangeReconnectIntervalAsync(uint reconnectInterval, CancellationToken cancellationToken)
        {
            ReconnectInterval = reconnectInterval;

            ServerSentEventBytes reconnectIntervalBytes = ServerSentEventsHelper.GetReconnectIntervalBytes(reconnectInterval);

            return(SendAsync(reconnectIntervalBytes, cancellationToken));
        }
コード例 #3
0
        /// <summary>
        /// Changes the interval after which clients will attempt to reestablish failed connections.
        /// </summary>
        /// <param name="reconnectInterval">The reconnect interval.</param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task ChangeReconnectIntervalAsync(uint reconnectInterval)
        {
            ReconnectInterval = reconnectInterval;

            ServerSentEventBytes reconnectIntervalBytes = ServerSentEventsHelper.GetReconnectIntervalBytes(reconnectInterval);

            return(ForAllClientsAsync(client => client.SendAsync(reconnectIntervalBytes)));
        }
        internal Task SendAsync(string groupName, ServerSentEventBytes serverSentEventBytes, Func <IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
        {
            if (_groups.ContainsKey(groupName))
            {
                return(SendAsync(_groups[groupName].Values.Where(clientPredicate), serverSentEventBytes, cancellationToken));
            }

            return(Task.CompletedTask);
        }
        internal Task SendAsync(string groupName, ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
        {
            if (_groups.ContainsKey(groupName))
            {
                return(SendAsync(_groups[groupName].Values, serverSentEventBytes, cancellationToken));
            }

            return(Task.CompletedTask);
        }
コード例 #6
0
        internal async Task <int> SendAsync(ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken, LogLevel minLogLevel = LogLevel.Information)
        {
            int clientCount = GetClientCount();
            int sentCount   = 0;

            if (clientCount > 0)
            {
                _logger.Log(minLogLevel, $"{DateTime.Now.ToString()} : SendAsync: Sending Event to {clientCount} Clients...");
                List <Task> clientsTasks = new List <Task>();
                foreach (ServerSentEventsClient client in _clients.Values)
                {
                    if (client.IsConnected)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        Task operationTask = client.SendAsync(serverSentEventBytes, cancellationToken, minLogLevel);
                        sentCount++;

                        if (operationTask.Status != TaskStatus.RanToCompletion)
                        {
                            clientsTasks.Add(operationTask);
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"SendAsync: Client: {client.Id} was not connected, Event was not sent");
                    }
                }
                await Task.WhenAll(clientsTasks);

                _logger.Log(minLogLevel, $"{DateTime.Now.ToString()} : SendAsync: Event sent to {sentCount} Connected Clients...");
                return(sentCount);
            }
            else
            {
                _logger.Log(minLogLevel, $"SendAsync: There are no Connected Clients");
            }
            return(sentCount);
        }
 internal Task SendAsync(ServerSentEventBytes serverSentEventBytes, Func <IServerSentEventsClient, bool> clientPredicate, CancellationToken cancellationToken)
 {
     return(SendAsync(_clients.Values.Where(clientPredicate), serverSentEventBytes, cancellationToken));
 }
 internal Task SendAsync(ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
 {
     return(SendAsync(_clients.Values, serverSentEventBytes, cancellationToken));
 }
コード例 #9
0
 internal Task SendEventAsync(ServerSentEventBytes serverSentEventBytes)
 {
     return(ForAllClientsAsync(client => client.SendAsync(serverSentEventBytes)));
 }
コード例 #10
0
 internal Task SendEventAsync(ServerSentEventBytes serverSentEventBytes, CancellationToken cancellationToken)
 {
     return(ForAllClientsAsync(client => client.SendAsync(serverSentEventBytes, cancellationToken), cancellationToken));
 }