-1

I have page which contains three panels for employee attendence, salary, production. I have to update the panels at real time by SignalR.
My hub class

public class MessagesHub : Hub
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["database"].ToString();

        [HubMethodName("sendMessages")]
        public void SendMessages()
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
            context.Clients.All.updateMessages();
        }
    }

In Controller

public ActionResult Panels()
{
   ...code..
   return Partial("_panelData", model);
}

The partial page has the panels along with data to display.
In the main View page(Panels), at script tag

<script>
        $(function () {
            // Declare a proxy to reference the hub.
            var notifications = $.connection.messagesHub;

            //debugger;
            // Create a function that the hub can call to broadcast messages.
            notifications.client.updateMessages = function () {
                getData()

            };
            // Start the connection.
            $.connection.hub.start().done(function () {
                getData();
            }).fail(function (e) {
                alert(e);
            });
        });

        function getData() {
            $.ajax({
                url: '/PanelController/Panels',
                type: 'GET',
                dataType: 'html'
            }).success(function (result) {
                $('.panel').html(result)

            }).error(function () {
                window.console.log("failure");
            });
        }
    </script>

It works fine when the page loads. But i also want it to load the data at every 1 min time interval.
Thanks in advance

anand
  • 1,559
  • 5
  • 21
  • 45

1 Answers1

1

Maybe I missunderstood but for me SignalR is useless there. Something like this should work :

 $(function() {  
   getData();
 });

 function getData() {
   $.ajax({
     url: '/PanelController/Panels',
     type: 'GET',
     dataType: 'html'
   }).success(function(result) {
     $('.panel').html(result)
     window.setTimeout(getData(), 60000)
   }).error(function() {
     window.console.log("failure");
   });

 }

If you want to keep signalr the only things to do is to add this: window.setTimeout(getData(), 60000) inside the callback of the getData function()

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36