【问题标题】:SignalR Connection Error details with Longpolling长轮询的 SignalR 连接错误详细信息
【发布时间】:2013-09-24 20:47:07
【问题描述】:

在 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,或者我可能返回的任何自定义状态代码,然后让我的应用做出相应响应...在这种情况下,请停止尝试重新连接并注销。

这是长轮询传输的错误处理程序:

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);
});

}

以及Server端集线器授权码

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 连接生命周期?

【问题讨论】:

    标签: javascript asp.net signalr long-polling


    【解决方案1】:
    1. 这是一个在 2.0 中修复的错误,它将在 1.1.5 中修复
    2. 是的,如果用户未获得授权,请确保不要呈现尝试建立 SignalR 连接的页面。

    【讨论】:

    • #1 - 很好,我能理解。 #2 - 这并没有回答关于使用 signalR 授权属性来授权连接的问题,它只是提供了一种不同的方法……我的应用程序目前不使用的方法。
    • 两者都可以吗?您将授权属性保留在集线器上,但您可以确保没有人可以访问该页面,除非他们也被授权,这样信号器客户端就不会为未经授权的用户发狂。如果这不是一个选项,那么唯一的另一种方法是自己更改信号器 javascript。
    • 好点,我实际上两者都做。我目前正在处理的问题是,使用相同的身份验证 cookie,测试人员在浏览器中克隆选项卡(大概所有这些选项卡都应该具有相同的有效 Asp.NET 身份验证 cookie)处于使 signalR 授权属性返回 403 的状态. 谢谢大卫。
    猜你喜欢
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多