/
GroupManager.cs
116 lines (100 loc) · 3.96 KB
/
GroupManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Infrastructure;
namespace Microsoft.AspNet.SignalR
{
/// <summary>
/// The default <see cref="IGroupManager"/> implementation.
/// </summary>
public class GroupManager : IConnectionGroupManager
{
private readonly IConnection _connection;
private readonly string _groupPrefix;
/// <summary>
/// Initializes a new instance of the <see cref="GroupManager"/> class.
/// </summary>
/// <param name="connection">The <see cref="IConnection"/> this group resides on.</param>
/// <param name="groupPrefix">The prefix for this group. Either a <see cref="IHub"/> name or <see cref="PersistentConnection"/> type name.</param>
public GroupManager(IConnection connection, string groupPrefix)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
_connection = connection;
_groupPrefix = groupPrefix;
}
/// <summary>
/// Sends a value to the specified group.
/// </summary>
/// <param name="groupName">The name of the group.</param>
/// <param name="value">The value to send.</param>
/// <returns>A task that represents when send is complete.</returns>
public Task Send(string groupName, object value, params string[] exclude)
{
if (groupName == null)
{
throw new ArgumentNullException("groupName");
}
var qualifiedName = CreateQualifiedName(groupName);
var message = new ConnectionMessage(qualifiedName, value)
{
ExcludedSignals = exclude
};
return _connection.Send(message);
}
/// <summary>
/// Adds a connection to the specified group.
/// </summary>
/// <param name="connectionId">The connection id to add to the group.</param>
/// <param name="groupName">The name of the group</param>
/// <returns>A task that represents the connection id being added to the group.</returns>
public Task Add(string connectionId, string groupName)
{
if (connectionId == null)
{
throw new ArgumentNullException("connectionId");
}
if (groupName == null)
{
throw new ArgumentNullException("groupName");
}
var command = new Command
{
Type = CommandType.AddToGroup,
Value = CreateQualifiedName(groupName),
WaitForAck = true
};
return _connection.Send(connectionId, command);
}
/// <summary>
/// Removes a connection from the specified group.
/// </summary>
/// <param name="connectionId">The connection id to remove from the group.</param>
/// <param name="groupName">The name of the group</param>
/// <returns>A task that represents the connection id being removed from the group.</returns>
public Task Remove(string connectionId, string groupName)
{
if (connectionId == null)
{
throw new ArgumentNullException("connectionId");
}
if (groupName == null)
{
throw new ArgumentNullException("groupName");
}
var command = new Command
{
Type = CommandType.RemoveFromGroup,
Value = CreateQualifiedName(groupName),
WaitForAck = true
};
return _connection.Send(connectionId, command);
}
private string CreateQualifiedName(string groupName)
{
return _groupPrefix + "." + groupName;
}
}
}