I have SignalR Hub for messages in Blazor Server Side project. In Hub i have list of users:
public static List<MessagesHubUser> _messagesHubUsers = new List<MessagesHubUser>();
public override async Task OnDisconnectedAsync(Exception e)
{
_messagesHubUsers.RemoveAll(x => x.ConnectionId == Context.ConnectionId);
await Clients.All.SendAsync("UsersListChanged");
await base.OnDisconnectedAsync(e);
}
But, when user just close a browser, in this case, the hub method "OnDisconnectedAsync" is not called. How to track that the user close a browser, to update userslist and notify other users?
I tried to make a counter so that it regularly triggers a Ping method on the client and returns true if the client is connected. In the absence of a response, the user was disconnected. But the call from the counter comes from outside the hub, which makes such a solution too difficult.