Progress report using SignalR and IProgress interface

后端 未结 2 851
一整个雨季
一整个雨季 2020-12-10 17:50

I have a Hub class, which has a long running method, I need to display a progress bar while it\'s working.

I read this article and I think it is possible to use IPro

2条回答
  •  情书的邮戳
    2020-12-10 18:32

    In your javascript add the following client method

     var appHub = $.connection.appHub;
      $.connection.hub.start();
      appHub.client.progress = function(progresspct) {
         // logic for whatever you want to do.
      };
    
      appHub.server.getServerTime()
              .done(function (time) {
                    alert(time);
               });
    

    Modify your server side code to this -

        public async Task GetServerTime(IProgress prog)
       {
        await Task.Run(() => {
            for (int i = 0; i < 10; i++)
            {
                prog.Report(i * 10);
                // Call to your client side method.
                Clients.Client(Context.ConnectionId).progress(i);
                System.Threading.Thread.Sleep(200);
            }
        });
    
        return DateTime.Now.ToString();
    }
    

提交回复
热议问题