Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 1a444a7

Browse files
committed
Working on GroupExcept
1 parent 31ef3c4 commit 1a444a7

File tree

10 files changed

+87
-3
lines changed

10 files changed

+87
-3
lines changed

samples/ChatSample/PresenceHubLifetimeManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,10 @@ public override Task RemoveGroupAsync(string connectionId, string groupName)
171171
{
172172
return _wrappedHubLifetimeManager.RemoveGroupAsync(connectionId, groupName);
173173
}
174+
175+
public override Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds)
176+
{
177+
return _wrappedHubLifetimeManager.InvokeGroupExceptAsync(groupName, methodName, args, excludedIds);
178+
}
174179
}
175180
}

src/Microsoft.AspNetCore.SignalR.Core/DefaultHubLifetimeManager.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ public override Task InvokeGroupAsync(string groupName, string methodName, objec
129129
return Task.CompletedTask;
130130
}
131131

132+
public override Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds)
133+
{
134+
if (groupName == null)
135+
{
136+
throw new ArgumentNullException(nameof(groupName));
137+
}
138+
139+
var group = _groups[groupName];
140+
if (group != null)
141+
{
142+
var message = CreateInvocationMessage(methodName, args);
143+
var tasks = group.Values.Where(connection => !excludedIds.Contains(connection.ConnectionId))
144+
.Select(c => c.WriteAsync(message));
145+
return Task.WhenAll(tasks);
146+
}
147+
148+
return Task.CompletedTask;
149+
}
150+
132151
private InvocationMessage CreateInvocationMessage(string methodName, object[] args)
133152
{
134153
return new InvocationMessage(GetInvocationId(), nonBlocking: true, target: methodName, argumentBindingException: null, arguments: args);

src/Microsoft.AspNetCore.SignalR.Core/HubCallerClients.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public IClientProxy Group(string groupName)
3939
return _hubClients.Group(groupName);
4040
}
4141

42+
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
43+
{
44+
return _hubClients.GroupExcept(groupName, excludeIds);
45+
}
46+
4247
public IClientProxy User(string userId)
4348
{
4449
return _hubClients.User(userId);

src/Microsoft.AspNetCore.SignalR.Core/HubContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public virtual IClientProxy Group(string groupName)
3737
return new GroupProxy<THub>(_lifetimeManager, groupName);
3838
}
3939

40+
public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
41+
{
42+
return new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds);
43+
}
44+
4045
public virtual IClientProxy User(string userId)
4146
{
4247
return new UserProxy<THub>(_lifetimeManager, userId);

src/Microsoft.AspNetCore.SignalR.Core/HubContext`T.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public virtual T Group(string groupName)
3939
return TypedClientBuilder<T>.Build(new GroupProxy<THub>(_lifetimeManager, groupName));
4040
}
4141

42+
public T GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
43+
{
44+
return TypedClientBuilder<T>.Build(new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludeIds));
45+
}
46+
4247
public virtual T User(string userId)
4348
{
4449
return TypedClientBuilder<T>.Build(new UserProxy<THub>(_lifetimeManager, userId));

src/Microsoft.AspNetCore.SignalR.Core/HubLifetimeManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public abstract class HubLifetimeManager<THub>
2020

2121
public abstract Task InvokeGroupAsync(string groupName, string methodName, object[] args);
2222

23+
public abstract Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds);
24+
2325
public abstract Task InvokeUserAsync(string userId, string methodName, object[] args);
2426

2527
public abstract Task AddGroupAsync(string connectionId, string groupName);

src/Microsoft.AspNetCore.SignalR.Core/IHubClients`T.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public interface IHubClients<T>
1515

1616
T Group(string groupName);
1717

18+
T GroupExcept(string groupName, IReadOnlyList<string> excludeIds);
19+
1820
T User(string userId);
1921
}
2022
}

src/Microsoft.AspNetCore.SignalR.Core/Proxies.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ public Task InvokeAsync(string method, params object[] args)
4040
}
4141
}
4242

43+
public class GroupExceptProxy<THub> : IClientProxy
44+
{
45+
private readonly string _groupName;
46+
private readonly HubLifetimeManager<THub> _lifetimeManager;
47+
private IReadOnlyList<string> _excludedIds;
48+
49+
public GroupExceptProxy(HubLifetimeManager<THub> lifetimeManager, string groupName, IReadOnlyList<string> excludedIds)
50+
{
51+
_lifetimeManager = lifetimeManager;
52+
_groupName = groupName;
53+
_excludedIds = excludedIds;
54+
}
55+
56+
public Task InvokeAsync(string method, params object[] args)
57+
{
58+
return _lifetimeManager.InvokeGroupExceptAsync(_groupName, method, args, _excludedIds);
59+
}
60+
}
61+
4362
public class AllClientProxy<THub> : IClientProxy
4463
{
4564
private readonly HubLifetimeManager<THub> _lifetimeManager;

src/Microsoft.AspNetCore.SignalR.Core/TypedHubClients.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public T Group(string groupName)
3232
return TypedClientBuilder<T>.Build(_hubClients.Group(groupName));
3333
}
3434

35+
public T GroupExcept(string groupName, IReadOnlyList<string> excludeIds)
36+
{
37+
return TypedClientBuilder<T>.Build(_hubClients.GroupExcept(groupName, excludeIds));
38+
}
39+
3540
public T User(string userId)
3641
{
3742
return TypedClientBuilder<T>.Build(_hubClients.User(userId));

src/Microsoft.AspNetCore.SignalR.Redis/RedisHubLifetimeManager.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,19 @@ public override Task InvokeGroupAsync(string groupName, string methodName, objec
190190
throw new ArgumentNullException(nameof(groupName));
191191
}
192192

193-
var message = new InvocationMessage(GetInvocationId(), nonBlocking: true, target: methodName, argumentBindingException: null, arguments: args);
193+
var message = new RedisExcludeClientsMessage(GetInvocationId(), nonBlocking: true, target: methodName, excludedIds: null, arguments: args);
194+
195+
return PublishAsync(_channelNamePrefix + ".group." + groupName, message);
196+
}
197+
198+
public override Task InvokeGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedIds)
199+
{
200+
if (groupName == null)
201+
{
202+
throw new ArgumentNullException(nameof(groupName));
203+
}
204+
205+
var message = new RedisExcludeClientsMessage(GetInvocationId(), nonBlocking: true, target: methodName, excludedIds: excludedIds, arguments: args);
194206

195207
return PublishAsync(_channelNamePrefix + ".group." + groupName, message);
196208
}
@@ -547,11 +559,16 @@ private Task SubscribeToGroup(string groupChannel, GroupData group)
547559
{
548560
try
549561
{
550-
var message = DeserializeMessage<HubInvocationMessage>(data);
562+
var message = DeserializeMessage<RedisExcludeClientsMessage>(data);
551563

552-
var tasks = new List<Task>(group.Connections.Count);
564+
var tasks = new List<Task>();
553565
foreach (var groupConnection in group.Connections)
554566
{
567+
if (message.ExcludedIds.Contains(groupConnection.ConnectionId))
568+
{
569+
continue;
570+
}
571+
555572
tasks.Add(groupConnection.WriteAsync(message));
556573
}
557574

0 commit comments

Comments
 (0)