Small tweaks to sample

- Fix redis issues with removing while
- Use string interpolation in the Chat hub
This commit is contained in:
David Fowler 2016-11-04 01:50:55 -07:00
parent 6b3d9bd96d
commit 9c10b89fa8
2 changed files with 22 additions and 9 deletions

View File

@ -8,27 +8,27 @@ namespace SocketsSample.Hubs
{
public override async Task OnConnectedAsync()
{
await Clients.All.InvokeAsync("Send", Context.Connection.ConnectionId + " joined");
await Clients.All.InvokeAsync("Send", $"{Context.Connection.ConnectionId} joined");
}
public override async Task OnDisconnectedAsync()
{
await Clients.All.InvokeAsync("Send", Context.Connection.ConnectionId + " left");
await Clients.All.InvokeAsync("Send", $"{Context.Connection.ConnectionId} left");
}
public Task Send(string message)
{
return Clients.All.InvokeAsync("Send", Context.ConnectionId + ": " + message);
return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
}
public Task SendToGroup(string groupName, string message)
{
return Clients.Group(groupName).InvokeAsync("Send", Context.ConnectionId + ": " + message);
return Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
}
public async Task JoinGroup(string groupName)
{
await Clients.Group(groupName).InvokeAsync("Send", Context.Connection.ConnectionId + " joined " + groupName);
await Clients.Group(groupName).InvokeAsync("Send", $"{Context.Connection.ConnectionId} joined {groupName}");
await Groups.AddAsync(groupName);
}
@ -37,7 +37,7 @@ namespace SocketsSample.Hubs
{
await Groups.RemoveAsync(groupName);
await Clients.Group(groupName).InvokeAsync("Send", Context.Connection.ConnectionId + " left " + groupName);
await Clients.Group(groupName).InvokeAsync("Send", $"{Context.Connection.ConnectionId} left {groupName}");
}
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -170,7 +171,9 @@ namespace Microsoft.AspNetCore.SignalR.Redis
if (groupNames != null)
{
foreach (var group in groupNames)
// Copy the groups to an array here because they get removed from this collection
// in RemoveGroupAsync
foreach (var group in groupNames.ToArray())
{
tasks.Add(RemoveGroupAsync(connection, group));
}
@ -184,7 +187,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis
var groupChannel = typeof(THub).FullName + "." + groupName;
var groupNames = connection.Metadata.GetOrAdd("group", _ => new HashSet<string>());
groupNames.Add(groupName);
lock (groupNames)
{
groupNames.Add(groupName);
}
var group = _groups.GetOrAdd(groupChannel, _ => new GroupData());
@ -234,7 +241,13 @@ namespace Microsoft.AspNetCore.SignalR.Redis
}
var groupNames = connection.Metadata.Get<HashSet<string>>("group");
groupNames?.Remove(groupName);
if (groupNames != null)
{
lock (groupNames)
{
groupNames.Remove(groupName);
}
}
await group.Lock.WaitAsync();
try