I currently save my users in a table called OnlineUsers. When a person connects or disconnects, he adds the user ID and connectionid to the table, but for some reason (I believe that when several browser windows open), the Disconnect function sometimes does not work, leaving users in the table and making them visible "on the Internet "" when they really aren't. Has anyone encountered this problem before and what would be a good way to solve this problem?
UPDATE ** (sorry for not setting the code, I should have done it first)
Here are my db functions for adding and removing from a table:
public bool ConnectUser(Guid UserId, String ConnectionId) { if (!Ent.OnlineUsers.Any(x => x.UserId == UserId && x.ConnectionId == ConnectionId)) { Ent.OnlineUsers.AddObject(new OnlineUser { UserId = UserId, ConnectionId = ConnectionId }); Ent.SaveChanges(); return true; } else return false; } public void DisconnectUser(Guid UserId, String ConnectionId) { if (Ent.OnlineUsers.Any(x => x.UserId == UserId && x.ConnectionId == ConnectionId)) { Ent.OnlineUsers.DeleteObject(Ent.OnlineUsers.First(x => x.UserId == UserId && x.ConnectionId == ConnectionId)); Ent.SaveChanges(); } }
Here is my task of connecting and disconnecting a hub class:
public Task Disconnect() { disconnectUser(); return null; } public Task Reconnect(IEnumerable<string> connections) { connectUser(); return null; } public Task Connect() { connectUser(); return null; } private void connectUser() { if (onlineUserRepository.ConnectUser(MainProfile.UserId, Context.ConnectionId)) { Groups.Add(Context.ConnectionId, Convert.ToString(MainProfile.ChatId)); } } private void disconnectUser() { onlineUserRepository.DisconnectUser(MainProfile.UserId, Context.ConnectionId); Groups.Remove(Context.ConnectionId, Convert.ToString(MainProfile.ChatId)); }
I checked that I am in the latest version of signalR (0.5.3), and this seems to happen when I open several browser windows and I close them all at once, users are stuck in the database.
If necessary, this is my connection identifier generator class:
public class MyConnectionFactory : IConnectionIdGenerator { public string GenerateConnectionId(IRequest request) { if (request.Cookies["srconnectionid"] != null) { return request.Cookies["srconnectionid"].ToString(); } return Guid.NewGuid().ToString(); } }