Nicklas Pouey-Winger
Nicklas Pouey-Winger

Reputation: 3023

Using javascript timers with signalR events to check for new data

I have this table, with a set of rows, each using a unique connection to signalR. This allows me to update several rows at the same time with unique content. The way it works is that a service bus provides the messagehub with new values and a uniqe id to go with that value, every time a remote unit transmits a new message.

At this point i'd like to run a check every 10 seconds to see if the webserver still gets a message from the unit, which transmits this as long as it is alive. In other words, if there's more than 10 seconds since the last time SignalR gave me a value, this would indicate that the connection to the remote unit is lost. (Not to be mistaken with SignalR losing its connection)

As I have a lot of units (rows) in my table, I was wondering if a javascript timer for each row would be sufficient for this check, or is there a better way of doing this? If so, do I do this in my connector script or in my html?

Upvotes: 0

Views: 117

Answers (2)

Nicklas Pouey-Winger
Nicklas Pouey-Winger

Reputation: 3023

Ok, so I figured this out in another way, letting my messagehandler take care of the task of distributing messages at the correct time:

    public class AsxActivityAliveEventMessageHandler : IHandleMessages<AsxActivityAliveEvent>
    {
        private const double INTERVAL = 10000;
        public static bool AsxConnected { get; set; }
        private static Dictionary<String, TagTimer> _connectionTimers = new Dictionary<string, TagTimer>();

        public void Handle(AsxActivityAliveEvent message)
        {
            AsxConnected = true;
            NotifyClients(message);

            TagTimer timer;

            if (_connectionTimers.ContainsKey(message.ConveyanceId))
            {
                timer = _connectionTimers[message.ConveyanceId];
                if (timer != null)
                {
                    timer.Stop();
                    timer.Elapsed -= timer_Elapsed;
                    _connectionTimers.Remove(message.ConveyanceId);
                }
            }

            timer = new TagTimer
                {
                    Interval = INTERVAL,
                    Tag = message
                };

            timer.Elapsed += timer_Elapsed;
            _connectionTimers.Add(message.ConveyanceId, timer);
            timer.Start();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var timer = sender as TagTimer;

            if (timer != null)
            {
                timer.Stop();
                timer.Elapsed -= timer_Elapsed;
            }

            AsxConnected = false;
            if (timer != null)
            {
                NotifyClients(timer.Tag as AsxActivityAliveEvent);
            }
        }

        static void NotifyClients(AsxActivityAliveEvent message)
        {
            var messageHub = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
            var conveyanceId = message.ConveyanceId;

            // Removed some vars and notify's as they're not relevant to this example

            messageHub.Clients.Group(message.ConveyanceId).notifyAlive(AsxConnected, conveyanceId);

        }
    }

    internal class TagTimer : Timer
    {
        public object Tag { get; set; }
    }
}

Upvotes: 0

Gustavo Armenta
Gustavo Armenta

Reputation: 1553

A single timer firing every 10 seconds and scanning all your signalr connections should work fine.

Upvotes: 1

Related Questions