简体   繁体   English

SignalR使用Chrome上的服务器发送事件

[英]SignalR using server-sent event on Chrome

My Problem: 我的问题:

I am running my code on Chrome, notice that SignalR is using html5 server-sent events as an automatic choice, great. 我在Chrome上运行我的代码,注意SignalR正在使用html5 server-sent events作为自动选择,很棒。 However, when I refresh the page onDisconnect() on server side doesn't fire until 30 seconds later. 但是,当我刷新服务器端的onDisconnect()页面时,直到30秒后才会触发。 Is there any settings in the Hub I can do to make it response upon the disconnect event immediately? Hub是否有任何设置可以立即对断开事件做出响应?

My Observation: 我的观察:

Note that delay issue only happens with I refresh page, not when I am closing the browser. 请注意,延迟问题仅在我刷新页面时发生,而不是在我关闭浏览器时。 The onDisconnect() fires instantly when closing the browser. 关闭浏览器时, onDisconnect()会立即触发。 Strange thing @_@ 奇怪的事情 @_@

Version Limitation: 版本限制:

I am using SignalR 1.1.3 (The latest version before 2.xx happened) because we don't have vs2013 in our company and I couldn't get 2.xx to work with vs2013 express on my pc anyway. 我使用的是SignalR 1.1.3(2.xx之前的最新版本),因为我们公司没有vs2013而且无论如何我都无法使用2.xx与vs2013 express一起工作。

Please help :) 请帮忙 :)

It's a bug. 这是一个错误。 One that we struggle with everytime a new version of a browser comes out we need to verify that thing still work. 我们在每次浏览器的新版本出现时都会遇到困难,我们需要验证该东西是否仍然有效。 Here's the outstanding bug for that issue https://github.com/SignalR/SignalR/issues/2719 这是该问题的突出错误https://github.com/SignalR/SignalR/issues/2719

SignalR sends an ajax request to OnDisconnected in the window.unload event to notify the server it aborted the connection. SignalR在window.unload事件中向OnDisconnected发送ajax请求,以通知服务器中止连接。 This ajax request may not complete before the document unloads if it is asynchronous, or it may simply not get executed by the browser at all. 如果文档是异步的,则在文档卸载之前可能无法完成此ajax请求,或者根本不会被浏览器执行。 This behavior often seems to change between different browser versions; 这种行为似乎在不同浏览器版本之间发生变化; I've seen this fail in various versions of Chrome and Firefox. 我在各种版本的Chrome和Firefox中都看到了这种情况。 In SignalR 2.x, the script also tries to stop the connection in onbeforeunload; 在SignalR 2.x中,脚本还尝试在onbeforeunload中停止连接; I'm not sure if that completely resolves the issue. 我不确定这是否完全解决了这个问题。

If said ajax call doesn't complete, OnDisconnected is only called after the disconnect timeout (30s by default). 如果所述ajax调用未完成,则仅在断开连接超时 (默认为30秒)后调用OnDisconnected You can change this setting via: 您可以通过以下方式更改此设置

GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30);

You could try modifying the SignalR script so it always uses async: false when aborting from window.unload, which may increase the odds of the call completing in your scenario. 您可以尝试修改SignalR脚本,以便在从window.unload中止时始终使用async: false ,这可能会增加您的方案中完成调用的几率。 However, doing a synchronous call from window.unload may also be considered bad practice by the browser because it can potentially keep the page from unloading. 但是,从window.unload执行同步调用也可能被浏览器视为不好的做法,因为它可能会使页面无法卸载。 So the browser may decide to not allow such a call. 因此浏览器可能决定不允许这样的呼叫。 I'm having the same problem with SignalR 2.0-rc1 in a self-hosted cross-domain setup and haven't been able to find a good solution so far. 我在自托管跨域设置中遇到了与SignalR 2.0-rc1相同的问题,到目前为止还没有找到一个好的解决方案。 So, ultimately it seems like it may not be wise to rely on an immediate call to OnDisconnected at all. 因此,最终似乎依赖于立即调用OnDisconnected可能并不明智。

One issue that I've noticed is that if you start the hub connection with jsonp: true , it won't notify the server at all even if you close the tab - in that case, setting the ajax call's dataType to "text" will at least be a partial fix. 我注意到的一个问题是,如果你使用jsonp: true启动集线器连接,即使关闭选项卡也不会通知服务器 - 在这种情况下,将ajax调用的dataType设置为“text”将至少是部分修复。

I see there is a more authorative answer now, but I'm posting this anyway. 我现在看到有一个更具说服力的答案,但无论如何我都会发布这个。

Edit: 编辑:

Using this here 在这里使用它

window.onbeforeunload = function (e) {
    $.connection.hub.stop();
};

works quite well, actually. 实际上工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM