// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information. using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.SignalR.Infrastructure; namespace Microsoft.AspNet.SignalR.Hubs { /// /// Encapsulates all information about an individual SignalR connection for an . /// public class HubConnectionContext : IHubConnectionContext { private readonly string _hubName; private readonly string _connectionId; private readonly Func, Task> _send; /// /// Initializes a new instance of the . /// public HubConnectionContext() { } /// /// Initializes a new instance of the . /// /// The pipeline invoker. /// The connection. /// The hub name. /// The connection id. /// The connection hub state. public HubConnectionContext(IHubPipelineInvoker pipelineInvoker, IConnection connection, string hubName, string connectionId, StateChangeTracker tracker) { _send = (signal, invocation, exclude) => pipelineInvoker.Send(new HubOutgoingInvokerContext(connection, signal, invocation, exclude)); _connectionId = connectionId; _hubName = hubName; Caller = new StatefulSignalProxy(_send, connectionId, PrefixHelper.HubConnectionIdPrefix, hubName, tracker); All = AllExcept(); Others = AllExcept(connectionId); } /// /// All connected clients. /// public dynamic All { get; set; } /// /// All connected clients except the calling client. /// public dynamic Others { get; set; } /// /// Represents the calling client. /// public dynamic Caller { get; set; } /// /// Returns a dynamic representation of all clients except the calling client ones specified. /// /// The list of connection ids to exclude /// A dynamic representation of all clients except the calling client ones specified. public dynamic AllExcept(params string[] excludeConnectionIds) { return new ClientProxy(_send, _hubName, PrefixHelper.GetPrefixedConnectionIds(excludeConnectionIds)); } /// /// Returns a dynamic representation of all clients in a group except the calling client. /// /// The name of the group /// A dynamic representation of all clients in a group except the calling client. public dynamic OthersInGroup(string groupName) { return Group(groupName, _connectionId); } /// /// Returns a dynamic representation of the specified group. /// /// The name of the group /// The list of connection ids to exclude /// A dynamic representation of the specified group. public dynamic Group(string groupName, params string[] excludeConnectionIds) { if (string.IsNullOrEmpty(groupName)) { throw new ArgumentException(Resources.Error_ArgumentNullOrEmpty, "groupName"); } return new GroupProxy(_send, groupName, _hubName, PrefixHelper.GetPrefixedConnectionIds(excludeConnectionIds)); } /// /// Returns a dynamic representation of the connection with the specified connectionid. /// /// The connection id /// A dynamic representation of the specified client. public dynamic Client(string connectionId) { if (string.IsNullOrEmpty(connectionId)) { throw new ArgumentException(Resources.Error_ArgumentNullOrEmpty, "connectionId"); } return new ConnectionIdProxy(_send, connectionId, _hubName); } } }