如何使用SignalR显示进度条[重复]。

问题描述 投票:1回答:1

当我在使用AJAX时,它是非常直接的。

  1. 启动一个进度条(比如提交按钮进度条)。
  2. 进行AJAX调用
  3. 当成功处理程序被调用时,用你从AJAX调用中得到的数据更新UI,并停止进度条。

当我用SignalR工作时,我做了以下工作。在服务器上,我的hum方法不返回任何东西(所以我不能更新UI和停止进度条)。相反,我为所有的客户端(包括发送数据的客户端)调用一个类似ThisDataWasChanged的方法。

我不太清楚该怎么做。我的意思是,我当然可以先返回调用中心方法的客户机的数据,然后更新所有其他客户机(以某种方式区分它们),但这似乎是矫枉过正。

asp.net-mvc signalr
1个回答
2
投票

你需要做的是在你的Hub类中加入类似以下的内容。

枢纽

public class ProgressBarHub : Hub
{
    public void change()
    {
        // Do some stuff
        Clients.Client(Context.ConnectionId).thisDataWasChanged(progressBarObj);   
    }
}

JS

// Create hub
var hub = $.connection.progressBarHub;

// Connect
$.connection.hub.start().done(function () {
    // Start progress bar?
    // Create a start progress bar method in the hub and call it
    });


hub.client.thisDataWasChanged = function (progressBarObj) {
    //Do something to the progress bar with the progressBarObj that we returned 
};
© www.soinside.com 2019 - 2024. All rights reserved.