与Longpolling的SignalR连接错误详细信息

时间:2013-09-19 16:29:28

标签: javascript asp.net signalr long-polling

在jquery.signalR-1.1.3.js中,longpolling错误函数传回响应文本而不是整个jqXHR对象。

是否有一种很好的方法可以在集线器错误处理程序中确定错误实际是什么?

$.connection.hub.error(function (error) {
            system.log('SignalR error: ' + error);
        });

我已经使用SignalR [Authorize]属性在应该拒绝连接时返回HTTP 403 Forbidden,但在我的客户端中,我得到的是错误对象中的IIS错误页面响应页面,因为SignalR不会传回整个响应对象,请参见下文:$(instance).triggerHandler(events.onError, [data.responseText]);。这种情况发生在一个紧密的循环中,因为SignalR只是不断尝试重新连接几百/千/次,直到重新连接超时通过。

我希望能够告诉它是403,或者我可能返回的任何自定义状态代码,然后让我的应用程序做出相应的响应......在这种情况下,退出尝试重新连接并注销。

这是longpolling传输的错误处理程序:

error: function (data, textStatus) {
// Stop trying to trigger reconnect, connection is in an error state
// If we're not in the reconnect state this will noop
window.clearTimeout(reconnectTimeoutId);
reconnectTimeoutId = null;

if (textStatus === "abort") {
    connection.log("Aborted xhr requst.");
    return;
}

// Increment our reconnect errors, we assume all errors to be reconnect errors
// In the case that it's our first error this will cause Reconnect to be fired
// after 1 second due to reconnectErrors being = 1.
reconnectErrors++;

if (connection.state !== signalR.connectionState.reconnecting) {
    connection.log("An error occurred using longPolling. Status = " + textStatus + ". " + data.responseText);
    $(instance).triggerHandler(events.onError, [data.responseText]);
}

// Transition into the reconnecting state
transportLogic.ensureReconnectingState(instance);

// If we've errored out we need to verify that the server is still there, so re-start initialization process
// This will ping the server until it successfully gets a response.
that.init(instance, function () {
    // Call poll with the raiseReconnect flag as true
    poll(instance, true);
});

}

服务器端集线器授权代码

public override bool AuthorizeHubConnection(Microsoft.AspNet.SignalR.Hubs.HubDescriptor hubDescriptor, Microsoft.AspNet.SignalR.IRequest request)
{
    // If we want them to connect, return true, else false
    // Asp.NET will return a 403 if we return false here
    return false;
}

出现了几个问题......

  1. 为什么signalR会像这样紧紧地重新连接?
  2. 是否有更好的方法可以根据集线器连接的授权来控制signalR连接生命周期?

1 个答案:

答案 0 :(得分:0)

  1. 这是一个在2.0中修复的错误,它将在1.1.5
  2. 中修复
  3. 是的,如果用户未获得授权,请确保不呈现尝试进行SignalR连接的页面。