It sometimes takes a second or more to connect to my SignalR server from the browser - even when running locally. I thought websockets were supposd to be fast!
问题:
回答1:
There is a configuration option to tell SignalR JS client to wait until the page load
event is complete before sending anything.
Just set waitForPageLoad: false
in the startup options to prevent this happening. Of course you must make sure that anything you do in the callback can be executed safely if the page isn't loaded.
Anything like a YouTube video not loading can delay the start - so I'm not sure why it isn't better/more widely documented!
$.connection.hub.start({ waitForPageLoad: false}).done(function() {
});
Excerpt from source code (which is how I discovered this):
// Check to see if start is being called prior to page load
// If waitForPageLoad is true we then want to re-direct function call to the window load event
if (!_pageLoaded && config.waitForPageLoad === true) {
connection._.deferredStartHandler = function () {
connection.start(options, callback);
};
_pageWindow.bind("load", connection._.deferredStartHandler);
return deferred.promise();
}
回答2:
Another possibility : Watch out that nothing else is blocking the browser, such as long running initialization code.
I use knockout.js and for some pages it has a particularly long initialization - blocking the browser and making it look like SignalR took several seconds whereas in fact it took just mere milliseconds.