将 ConnectionId 添加到组时,我丢失了 Hub 中的所有连接
I lost all connections in Hub when I'm adding ConnectionId to group
我正在开发我的 SignalR Hub。
我想在触发 OnConnectedAsync()
时将 Context.ConnectionId
添加到特定组。
当我这样写的时候:
public override Task OnConnectedAsync()
{
// succeeded adding ConnectionId to User in my cache
if (succeeded)
{
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id2);
}
}
我在集线器上失去了所有连接。当我删除这个 AddToGroupAsync()
部分时,一切正常(存在连接但不在组中)。
为什么当我使用 AddToGroupAsync()
添加 ConnectionId
时,我用这个简单的方法丢失了所有连接?
我没有找到解决该问题的方法,所以我会在这里回答。
当您触发该方法时:
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
您需要检查 user.Id1
是否不为空。当 SignalR Hub 似乎无法处理时,他从集线器中删除了连接。
在将连接 ID 添加到组之前检查一下,如果字符串不为空:
if (user.Id1 != null) // or string.IsNullOrEmpty(user.Id1)
{
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
}
我正在开发我的 SignalR Hub。
我想在触发 OnConnectedAsync()
时将 Context.ConnectionId
添加到特定组。
当我这样写的时候:
public override Task OnConnectedAsync()
{
// succeeded adding ConnectionId to User in my cache
if (succeeded)
{
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id2);
}
}
我在集线器上失去了所有连接。当我删除这个 AddToGroupAsync()
部分时,一切正常(存在连接但不在组中)。
为什么当我使用 AddToGroupAsync()
添加 ConnectionId
时,我用这个简单的方法丢失了所有连接?
我没有找到解决该问题的方法,所以我会在这里回答。
当您触发该方法时:
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
您需要检查 user.Id1
是否不为空。当 SignalR Hub 似乎无法处理时,他从集线器中删除了连接。
在将连接 ID 添加到组之前检查一下,如果字符串不为空:
if (user.Id1 != null) // or string.IsNullOrEmpty(user.Id1)
{
await Groups.AddToGroupAsync(Context.ConnectionId, user.Id1);
}