简体   繁体   English

如何确定SignalR客户端上是否有待处理的请求?

[英]How to find out if there is a request pending on SignalR client?

I'm trying to figure out whether there are any pending requests on the SignalR client so that I can show a "Loading" indicator until all of the requests are processed. 我试图弄清楚SignalR客户端上是否有任何待处理的请求,以便在所有请求得到处理之前,我可以显示“正在加载”指示符。 Here is what I am doing currently: 这是我目前正在做的事情:

// some variable to hold whether there are pending requests
var isProcessing = false;


function ProcessSomeData(data)
{
    isProcessing = true;

    hub.server.setUserData(data).done(function () {
        isProcessing = false;
    });
}

Then I look at isProcessing to see if all the requests are processed. 然后,我查看isProcessing以查看是否所有请求都已处理。 This is inaccurate (sometimes isProcessing gets set to false when there are still requests in progress), so I was wondering if there is a way to determine whether the SignalR client is "busy" processing requests or just idle. 这是不准确的(有时仍有正在处理的请求将isProcessing设置为false),所以我想知道是否有一种方法可以确定SignalR客户端是“忙”于处理请求还是只是空闲。 Any ideas? 有任何想法吗?

I don't know any other solution in SignalR library but I did some workaround for this behaviour before. 我不知道SignalR库中的任何其他解决方案,但之前我对此行为做了一些解决方法。 I send a loading message before send a real data from hub. 在从集线器发送真实数据之前,我先发送加载消息。 Code is like this: 代码是这样的:

public void Send(string message)
{
     var username = Context.User.Identity.Name;
     context.Clients.User(username).loadingMessages();
     //-- Do you process here
     context.Clients.User(username).yourMessage(message);
}

And at javascript you should create loadingMessages implementation: 在javascript中,您应该创建loadingMessages实现:

// some variable to hold whether there are pending requests
var isProcessing = false;

$.connection.hub.client.loadingMessages = function(){
   isProcessing = true;
   //-- Some Dom manupulation may be
}

function ProcessSomeData(data)
{


    hub.server.setUserData(data).done(function () {
       isProcessing = false;
    });
}

This is not a best solution but may be give you some approach.Thanks. 这不是最佳解决方案,但可以为您提供一些方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM