简体   繁体   中英

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. However, when I refresh the page onDisconnect() on server side doesn't fire until 30 seconds later. Is there any settings in the Hub I can do to make it response upon the disconnect event immediately?

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. 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.

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

SignalR sends an ajax request to OnDisconnected in the window.unload event to notify the server it aborted the connection. 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. This behavior often seems to change between different browser versions; I've seen this fail in various versions of Chrome and Firefox. In SignalR 2.x, the script also tries to stop the connection in 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). 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. 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. 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. So, ultimately it seems like it may not be wise to rely on an immediate call to OnDisconnected at all.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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