【问题标题】:Notify browser UI about SignalR requests to the server通知浏览器 UI 关于 SignalR 对服务器的请求
【发布时间】:2017-03-17 14:24:19
【问题描述】:

我会在浏览器中使用 SignalR。一些请求(服务器上的调用函数)很长,我想显示微调器/加载栏。

我可以在这个函数启动和返回时以某种方式挂钩一个事件吗?

【问题讨论】:

    标签: signalr signalr.client


    【解决方案1】:

    我试图弄清楚你的意思 - 我认为基本上你想要某种方式来连接通话的开始和结束(加载和卸载微调器)?

    我以两种不同的方式完成了这项工作 - 首先是一次性的(第一个示例),然后是更系统的(第二个示例)。希望其中之一将是您所需要的。

    $.connection.myHub.server.hubMethod().done(function () {
                                    //called on success
                                }).fail(function (e) {
                                    //called on failure - I don't recommend reading e
                                }).always(function() {
                                    //called regardless
                                    spinner.close();
    });
    spinner.open(); // must be triggerd AFTER call incase exception thrown (due to connection not being up yet)
    

    如果您不喜欢这样 - 可能是因为您在数百个不同的代码部分中调用了 hub 方法,那么还有其他一些更复杂的技巧。让我们看看:

    function SetupSpinnerOnCallToSignalrMethod(hubServer, method, spinnerStartCallback, spinnerEndCallback) {
        var prevFunc = hubServer[method];
        hubServer[method] = function () {
            var ret = prevFunc.apply(this, arguments);
            spinnerStartCallback(); // must be triggerd AFTER call incase exception thrown (due to connection not being up yet)
            ret.always(function() {
                spinnerEndCallback();
            });
            return ret;
        };
    }
    
    //then call this for each method
    SetupSpinnerOnCallToSignalrMethod($.connection.myHub.server,
                                      "hubMethod",
                                      function() { spinner.open(); },
                                      function() { spinner.close(); }
    );
    
    //the server call should then work exactly as before, but the spinner open and close calls are invoked each time.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多