【发布时间】:2016-06-10 15:12:49
【问题描述】:
我注意到在 Firefox 中,我的 Signal R 集线器的断开连接事件被延迟。我相当确定这是因为未收到来自客户端的断开连接事件,而是服务器超时。如果我调试断开连接事件,stopCalled 参数始终为 false。
我尝试在 beforeunload 事件上从我的 JavaScript 手动调用停止事件,但这没有任何效果。我读到 Firefox 出于安全原因不允许在卸载事件中使用同步事件,所以它可能会阻止调用?
CS
public class WebHub : Hub
{
public override Task OnDisconnected(bool stopCalled) { }
}
JS
$.connection.webHub.start().done();
$(window).bind('beforeunload', function() {
$.connection.hub.stop();
});
如果我在 Firefox 控制台中调用 stop 方法,它会立即触发断开连接事件。
更新 1
打开 SignalR JS 日志记录后,似乎断开连接发生在客户端,只是没有访问服务器。
//navigated page, connection ends
SignalR: Stopping connection." jquery.signalR-2.2.0.js:81:17
SignalR: EventSource calling close()." jquery.signalR-2.2.0.js:81:17
SignalR: Fired ajax abort async = true." jquery.signalR-2.2.0.js:81:17
SignalR: Stopping the monitoring of the keep alive." jquery.signalR-2.2.0.js:81:17
SignalR: Window unloading, stopping the connection." jquery.signalR-2.2.0.js:81:17
//next page load begins
SignalR: Client subscribed to hub 'webhub'." jquery.signalR-2.2.0.js:81:17
SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5 jquery.signalR-2.2.0.js:81:17
SignalR: serverSentEvents transport starting." jquery.signalR-2.2.0.js:81:17
SignalR: Attempting to connect to SSE endpoint 'http://website.co.uk/signalr/connect?transport=serverSentEvents&clientProtocol=1.5 jquery.signalR-2.2.0.js:81:17
SignalR: EventSource connected." jquery.signalR-2.2.0.js:81:17
SignalR: serverSentEvents transport connected. Initiating start request." jquery.signalR-2.2.0.js:81:17
SignalR: The start request succeeded. Transitioning to the connected state." jquery.signalR-2.2.0.js:81:17
SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000" jquery.signalR-2.2.0.js:81:17
【问题讨论】:
-
您是否添加了断点以查看断开连接实际上被调用了? JS 控制台中有什么相关的吗?
-
是的,我添加了日志消息以确保在 JS 控制台中调用 .stop 方法。我可以确认它确实被调用了,但是我放在服务器端断开连接事件上的调试点直到经过明显延迟才会触发。
标签: javascript c# signalr