【问题标题】:Signal R Disonnect delayed in FirefoxFirefox 中的 Signalr Disconnect 延迟
【发布时间】: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


【解决方案1】:

我在使用 FireFox 86 时遇到了同样的问题。花了将近 2-3 天,但没有找到任何具体的解决方案。这是一种对我有用的解决方法。当我的浏览器是 firefox 并且它可以工作时,我只是忽略了 $.connection.hub.stop()。

$.connection.webHub.start().done();
$(window).bind('beforeunload', function() {
    if (navigator.userAgent.indexOf("Firefox") == -1) {
            $.connection.hub.stop();
        }
});

【讨论】:

    【解决方案2】:

    问题已通过删除 beforeunload 事件绑定得到解决。我最初添加了这个,因为它解决了 IE 中的类似问题。我现在已将其更改为仅在 IE 中运行。

    所以问题中的 JS 就变成了:-

    $.connection.webHub.start().done();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2014-04-08
      相关资源
      最近更新 更多