locked
Why does TransportHeartBeat.GetConnections return an empty list for a started hub, with connected clients? RRS feed

  • Question

  • User730391551 posted

    I have got a simple web application that allows users to chat. It also records the users connections in a table. I wanted to make sure that any dead connections were removed from this table.

    I stumbled upon a Presence Monitor sample that will do just this:

    https://patdunn.net/2015/05/05/building-a-simple-presence-manager-for-signalr/

    public class PresenceManager
    {
    private const int zombiePeriodThreshold = 2;
    private readonly ITransportHeartbeat _heartbeat;
    private readonly TimeSpan _checkInterval = TimeSpan.FromSeconds(60);
    private readonly int _zombieThreshold;
    private Timer timer;

    public PresenceManager(ITransportHeartbeat heartbeat)
    {
    _heartbeat = heartbeat;
    _zombieThreshold = (int)_checkInterval.TotalSeconds * zombiePeriodThreshold;
    }

    public void StartMonitoring()
    {
    if (timer == null)
    {
    timer = new Timer(t =>
    {
    try
    {
    Check();
    }
    catch (Exception ex)
    {
    Trace.TraceError(ex.Message);
    }
    }, null, TimeSpan.Zero, _checkInterval);
    }
    }

    //EF6 adds much better support for contains in queries http://bit.ly/1wmQ2VQ
    private void Check()
    {
    using (var db = new ApplicationContext())
    {
    //Update active connections
    var activeConnections = _heartbeat.GetConnections().Where(b => b.IsAlive).Select(b => b.ConnectionId);
    db.ConnectedClients
    .Where(c => activeConnections.Contains(c.ConnectionId))
    .ToList()
    .ForEach(c => c.LastActivity = DateTimeOffset.UtcNow);

    //Remove zombie connections
    var zombies = db.ConnectedClients.Where(c => SqlFunctions.DateDiff("ss", c.LastActivity, DateTimeOffset.UtcNow) >= _zombieThreshold).ToList();
    var zombieUserIDs = zombies.Select(b => b.ApplicationUserID);
    db.Users
    .Where(c => zombieUserIDs.Contains(c.Id))
    .Where(c => c.ConnectedClients.Count <= 1)
    .ToList()
    .ForEach(u => u.Status = UserStatus.Offline);

    db.ConnectedClients.RemoveRange(zombies);
    db.SaveChanges();
    }
    }

    }

    The problem i have is that in the Check method 

    var activeConnections = _heartbeat.GetConnections().Where(b => b.IsAlive).Select(b => b.ConnectionId);

    GetConnections() allways returns an empty list, despite the fact that hub.start has been called and users are chatting.

    Having done a little investigation i know that the interface has the method 

    AddOrUpdateConnection 

    but I'm assuming this is being called under the hood and i do not need to call this myself.

    I'm using SignalR v2.2.1.0.

    Any help would be much appreciated.

    Omar

    Friday, October 14, 2016 8:27 AM

All replies

  • User283571144 posted

    Hi oraman,

    I have got a simple web application that allows users to chat. It also records the users connections in a table. I wanted to make sure that any dead connections were removed from this table.

    I stumbled upon a Presence Monitor sample that will do just this:

    According to your description, I couldn't directly find the reason why the GetConnections method get the empty list.

    I guess there are something wrong with your enable tracing in SignalR.

    Could you please post more relevant codes about how you use this PresenceManager?

    Which parameters you send to this PresenceManager?

    If you could post more relevant codes, it will be more easily for us to find the problem.

    Best Regards,

    Brando

    Monday, October 17, 2016 7:14 AM