Turn off the failure sometimes in the signal

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(); } } 
+4
source share
1 answer

I think your factory connection is really a problem. If the case when you do not find a cookie, you start and generate a new guid, but by then it is too late.

I understand that the connection identifier is set by the client (client concentrator) during initialization and cannot be changed on the server; it can only be read. In fact, when you return a new Guid, when you do not find a cookie, you change the client ID.

In my factory link, if no cookie is found, I quit. In the action of the controller that opens the page using signalr, I make sure the cookie is set.

Here is my factory connection:

 public class ConnectionFactory : IConnectionIdGenerator { public string GenerateConnectionId(IRequest request) { if (request.Cookies["UserGuid"] != null) return request.Cookies["UserGuid"].Value; throw new ApplicationException("No User Id cookie was found on this browser; you must have cookies enabled to enter."); } } 
+7
source

Source: https://habr.com/ru/post/1444232/


All Articles