【发布时间】: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;
}
出现了几个问题...
- 为什么 signalR 会像这样在一个紧密的循环中重新连接?
- 是否有更好的方法来控制基于集线器连接授权的 signalR 连接生命周期?
【问题讨论】:
标签: javascript asp.net signalr long-polling