public async ValueTask HandleAsync(ConnectedClient sender, Typed message, CancellationToken cancellationToken) { _state.UpdateProgress(sender.Group, sender.ClientId, message.TypedCharactersCount); foreach (var client in _connectedClients.FindInGroups(sender.Groups)) { if (client.ClientId == sender.ClientId) { continue; } /*await client.Connection.SendAsync(new Typed * { * TypedCharactersCount = message.TypedCharactersCount, * ClientId = sender.ClientId * }, cancellationToken) * .ConfigureAwait(false);*/ await _debouncer.SendAsync(client, new Typed { TypedCharactersCount = message.TypedCharactersCount, ClientId = sender.ClientId }).ConfigureAwait(false); } }
public ValueTask HandleAsync(ConnectedClient sender, BroadcastMessage message, CancellationToken cancellationToken) { message.SenderId = sender.ClientId; var clients = _connectedClients.FindInGroups(sender.Groups); return(AsyncHelpers.WhenAll(clients .Except(new[] { sender }) .Select(c => c.Connection.SendAsync(message, cancellationToken)))); }
public object GetUpdateFor(string clientId) { var playerId = new PlayerId(clientId); var player = _playerRepository.Find(playerId); if (player == null) { throw new InvalidOperationException($"Player {playerId} is not found."); } var state = player.GetState(); if (state.RoadMovementState != null) { var playerPositions = _connectedClients.FindInGroups(player.MessagingGroup) .Select(client => { var playerId = new PlayerId(client.ClientId); var player = _playerRepository.Find(playerId); if (player == null) { throw new InvalidOperationException($"Player {playerId} is not found."); } return(player); }) .Select(p => ToPlayerPosition(p, state.RoadMovementState.MovementDirection)); return(new MovementUpdate( state.RoadMovementState.RoadId, playerPositions.Single(p => p.PlayerId == state.PlayerId), playerPositions.ToList())); } var visiblePlayerIds = _connectedClients.FindInGroups(player.MessagingGroup) .Select(client => client.ClientId) .ToList(); return(new Update(state.LocationId, visiblePlayerIds)); }
private async ValueTask TrySendPendingUpdates(IEnumerable <string> groups, CancellationToken cancellationToken) { try { _updateDetector.MarkForUpdate(groups); var clientsThatNeedUpdate = _connectedClients.FindInGroups(_updateDetector.PopMarked()).ToList(); await AsyncHelpers.WhenAll(clientsThatNeedUpdate .Select(c => _updater.SendUpdateAsync(c, cancellationToken))).ConfigureAwait(false); } catch (Exception exception) { // TODO: Disconnect player if update was unsuccessful. Currently it silently continues working (investigate). _logger.LogError(exception, "Error during sending pending updates."); } }
/// <summary> /// Sends message to all the clients in specified list of groups. /// </summary> /// <param name="store">ConnectedClientStore instance.</param> /// <param name="message">Message to send to all the clients in given group.</param> /// <param name="groups">The list of groups to which the message will be sent.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns></returns> public static ValueTask SendAsync(this IConnectedClientStore store, object message, IEnumerable <string> groups, CancellationToken cancellationToken) => AsyncHelpers.WhenAll(store.FindInGroups(groups) .Select(x => x.Connection.SendAsync(message, cancellationToken)));
/// <summary> /// Finds all clients that are in given messaging group. /// </summary> /// <param name="store">ConnectedClientStore instance.</param> /// <param name="groups">Collection of messaging groups to search for.</param> /// <returns>Collection of clients that are present in requested group.</returns> public static IEnumerable <ConnectedClient> FindInGroups(this IConnectedClientStore store, params string[] groups) => store.FindInGroups(groups);