SignalR - Establishing Connection Taking quite a long time

徘徊边缘 提交于 2019-12-22 12:20:15

问题


I have just started working with SignalR for realtime update. I need to perform some operation after the connection is established. so I have written that code in .done() method.

The issue I am facing is : establishing connection takes some time and due to that I am not able to perform the operation which I am intended to. Following code snippet of my js.

   $(function () {
   // Proxy created on the fly  
    projectHub = $.connection.projectHub;
    $.connection.hub.logging = true;
    // Start the connection
    $.connection.hub.start().done(function () {
    $("#lnkFollowProject").live("click", function () {
        console.log("Follow click");
        projectHub.server.followProject(projectId, currentLoggedInUserId);
     });
     $(".lnkUnfollowProject").live("click", function () {
        console.log("Unfollow click");
        projectHub.server.unfollowProject(projectId, currentLoggedInUserId);
      });     });     });

I am not sure why it is taking time to establish connection. As i tried with simple chat application and it works well. I am doing many other functionality for that page and also using knockout for binding. (not for the above functionality). I have also searched this and found antivirus can be victim. I tried disabling that too but no gain.

I know one solution that I can disable button til connection is establish and on done enable it. But i don't want user to restrict.

Please update me if any other probable issues.

Thanks in Advance.


回答1:


I have two suggestions about the code performance.

1) Use .on() method instead of .live()

Using the .live() method is no longer recommended since the later versions of jQuery offer .delegate and .on methods. the following issues arise with the use of .live():

I) jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.

II) Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.

2) Take click events out of .done() method, because "followProject" and "unfollowProject" methods both need click event to tiger themselves first.

See below code:

$(function () {
            // Proxy created on the fly  
            projectHub = $.connection.projectHub;
            $.connection.hub.logging = true;
            // Start the connection
            $.connection.hub.start();

            $(document.body).on("click","#lnkFollowProject", function () {
               console.log("Follow click");
               projectHub.server.followProject(projectId, currentLoggedInUserId);
           });
           $(document.body).on("click",".lnkUnfollowProject", function () {    
               console.log("Unfollow click");
               projectHub.server.unfollowProject(projectId, currentLoggedInUserId);
           });       
        });


来源:https://stackoverflow.com/questions/20190568/signalr-establishing-connection-taking-quite-a-long-time

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