In OnConnected, the method client is added to the group with its name (the group contains the entire client identifier), then its name is added to the list if it does not exist.
static List<string> onlineClients = new List<string>();
public override Task OnConnected()
{
Groups.Add(Context.ConnectionId, Context.User.Identity.Name);
if (!onlineClients.Exists(x => x == Context.User.Identity.Name))
{
onlineClients.Add(Context.User.Identity.Name);
}
return base.OnConnected();
}
In the OnDisconnected method, I try to check if a group is empty to remove an item from the list. But after removing the last compound, the group is not equal to zero.
public override Task OnDisconnected(bool stopCalled)
{
if (stopCalled)
{
Groups.Remove(Context.ConnectionId, Context.User.Identity.Name);
if (Clients.Group(Context.User.Identity.Name) == null)
{
onlineClients.Remove(Context.User.Identity.Name);
}
}
return base.OnDisconnected(stopCalled);
}
Is it possible to check an empty group?
source
share