SignalR is slow to connect from javascript client

≯℡__Kan透↙ 提交于 2019-12-04 11:41:51

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();
        }

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!