Small tweaks to sample
- Fix redis issues with removing while - Use string interpolation in the Chat hub
This commit is contained in:
parent
6b3d9bd96d
commit
9c10b89fa8
|
|
@ -8,27 +8,27 @@ namespace SocketsSample.Hubs
|
||||||
{
|
{
|
||||||
public override async Task OnConnectedAsync()
|
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()
|
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)
|
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)
|
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)
|
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);
|
await Groups.AddAsync(groupName);
|
||||||
}
|
}
|
||||||
|
|
@ -37,7 +37,7 @@ namespace SocketsSample.Hubs
|
||||||
{
|
{
|
||||||
await Groups.RemoveAsync(groupName);
|
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}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
@ -170,7 +171,9 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
|
|
||||||
if (groupNames != null)
|
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));
|
tasks.Add(RemoveGroupAsync(connection, group));
|
||||||
}
|
}
|
||||||
|
|
@ -184,7 +187,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
var groupChannel = typeof(THub).FullName + "." + groupName;
|
var groupChannel = typeof(THub).FullName + "." + groupName;
|
||||||
|
|
||||||
var groupNames = connection.Metadata.GetOrAdd("group", _ => new HashSet<string>());
|
var groupNames = connection.Metadata.GetOrAdd("group", _ => new HashSet<string>());
|
||||||
groupNames.Add(groupName);
|
|
||||||
|
lock (groupNames)
|
||||||
|
{
|
||||||
|
groupNames.Add(groupName);
|
||||||
|
}
|
||||||
|
|
||||||
var group = _groups.GetOrAdd(groupChannel, _ => new GroupData());
|
var group = _groups.GetOrAdd(groupChannel, _ => new GroupData());
|
||||||
|
|
||||||
|
|
@ -234,7 +241,13 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
}
|
}
|
||||||
|
|
||||||
var groupNames = connection.Metadata.Get<HashSet<string>>("group");
|
var groupNames = connection.Metadata.Get<HashSet<string>>("group");
|
||||||
groupNames?.Remove(groupName);
|
if (groupNames != null)
|
||||||
|
{
|
||||||
|
lock (groupNames)
|
||||||
|
{
|
||||||
|
groupNames.Remove(groupName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await group.Lock.WaitAsync();
|
await group.Lock.WaitAsync();
|
||||||
try
|
try
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue