SignalR is slow to connect from javascript client

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

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.



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